一,在Yii中使用session
1,CHttpSession
與原生態php5的session使用差異是,php5使用session_start();$_session['key'] = $value;
在yii中,session已經被封裝。
To start the session, call open(); To complete and send out session data, call close(); To destroy the session, call destroy().
If autoStart is set true, the session will be started automatically when the application component is initialized by the application. php
/***** 方式1、實例添加 *****/ $session=new CHttpSession; $session->open(); $value1=$session['name1']; /***** 方式2、直接調用應用添加 *****/ Yii::app()->session->add('name','foobar'); Yii::app()->session->add('name2','foobar'); Yii::app()->session->add('name3','foobar'); //或者 $session = Yii::app()->session; $session['key'] = 'value'; var_dump($session['key']); //遍歷 foreach($session as $name=>$value)
$session = new CHttpSession; $session->open(); $user_id = $this->user->id; $sessionKey = $user_id.'_is_sending'; if(isset($session[$sessionKey])){ $first_submit_time = $session[$sessionKey]; $current_time = time(); if($current_time - $first_submit_time < 10){ $session[$sessionKey] = $current_time; $this->response(array('status'=>1, 'msg'=>'不能在10秒鐘內連續發送兩次。')); }else{ unset($session[$sessionKey]);//超過限制時間,釋放session"; } } //第一次點擊確認按鈕時執行 if(!isset($session[$sessionKey])){ $session[$sessionKey] = time(); } var_dump($sessionKey);var_dump($session[$sessionKey]);exit();
在index.php
在$app->run();前 html
$session = Yii::app()->session; session_set_save_handler( array($session,'openSession'), array($session,'closeSession'), array($session,'readSession'), array($session,'writeSession'), array($session,'destroySession'), array($session,'gcSession') );
2,CDbHttpSession
CDbHttpSession繼承自 CHttpSession ,把session數據存儲在數據庫中(表名是YiiSession),
The table name can be changed by setting sessionTableName. If the table does not exist, it will be automatically created if autoCreateSessionTable is set true.
The following is the table structure:
CREATE TABLE YiiSession
(
id CHAR(32) PRIMARY KEY,
expire INTEGER,
data TEXT
)
CDbHttpSession relies on PDO to access database.
By default, it will use an SQLite3 database named 'session-YiiVersion.db' under the application runtime directory. You can also specify connectionID so that it makes use of a DB application component to access database.
When using CDbHttpSession in a production server, we recommend you pre-create the session DB table and set autoCreateSessionTable to be false. This will greatly improve the performance. You may also create a DB index for the 'expire' column in the session table to further improve the performance. 數據庫
CREATE TABLE `YiiSession` ( `id` char(32) NOT NULL, `expire` int(11) default NULL, `data` text, PRIMARY KEY (`id`), KEY `expire` (`expire`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
例,在../config/main.php中配置 cookie
'session'=>array( 'class' => 'CDbHttpSession', 'autoStart' => true, 'sessionTableName'=>'YiiSession', 'autoCreateSessionTable'=> false, 'connectionID'=>'db', ),
二,在Yii中使用cookie
Yii實現了一個cookie驗證機制,能夠防止cookie被修改。啓用以後能夠對cookie的值進行HMAC檢查。
Cookie驗證在默認狀況下是禁用的。若是你要啓用它,能夠編輯應用配置 中的組件中的CHttpRequest部分。
必定要使用通過Yii驗證過的cookie數據。使用Yii內置的cookies組件來進行cookie操做,不要使用$_COOKIES。session
// 檢索一個名爲$name的cookie值 $cookie=Yii::app()->request->cookies[$name]; $value=$cookie->value; ...... // 設置一個cookie $cookie=new CHttpCookie($name,$value); Yii::app()->request->cookies[$name]=$cookie;