PHP yii學習3

一,在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.

Java代碼  收藏代碼
  1. /***** 方式一、實例添加 *****/

  2. $session=new CHttpSession;  

  3. $session->open();  

  4. $value1=$session['name1'];  

  5. /***** 方式二、直接調用應用添加 *****/

  6. Yii::app()->session->add('name','foobar');  

  7. Yii::app()->session->add('name2','foobar');  

  8. Yii::app()->session->add('name3','foobar');  

  9. //或者

  10. $session = Yii::app()->session;  

  11. $session['key'] = 'value';  

  12. var_dump($session['key']);  

  13. //遍歷

  14. foreach($session as $name=>$value)  

一個實例

Java代碼  收藏代碼
  1. $session = new CHttpSession;  

  2. $session->open();  

  3. $user_id = $this->user->id;  

  4. $sessionKey = $user_id.'_is_sending';  

  5. if(isset($session[$sessionKey])){  

  6.    $first_submit_time = $session[$sessionKey];  

  7.    $current_time      = time();  

  8. if($current_time - $first_submit_time < 10){  

  9.        $session[$sessionKey] = $current_time;  

  10.        $this->response(array('status'=>1, 'msg'=>'不能在10秒鐘內連續發送兩次。'));  

  11.    }else{  

  12.        unset($session[$sessionKey]);//超過限制時間,釋放session";

  13.    }  

  14. }  

  15. //第一次點擊確認按鈕時執行

  16. if(!isset($session[$sessionKey])){  

  17.    $session[$sessionKey] = time();  

  18. }  

  19. var_dump($sessionKey);var_dump($session[$sessionKey]);exit();  

在index.php

在$app->run();前

Java代碼  收藏代碼
  1. $session = Yii::app()->session;  

  2. session_set_save_handler(  

  3.    array($session,'openSession'),  

  4.    array($session,'closeSession'),  

  5.    array($session,'readSession'),  

  6.    array($session,'writeSession'),  

  7.    array($session,'destroySession'),  

  8.    array($session,'gcSession')  

  9. );  

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.

Sql代碼  收藏代碼
  1. CREATETABLE `YiiSession` (  

  2.  `id` char(32) NOTNULL,  

  3.  `expire` int(11) defaultNULL,  

  4.  `data` text,  

  5. PRIMARYKEY  (`id`),  

  6. KEY `expire` (`expire`)  

  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  

例,在../config/main.php中配置

Php代碼  收藏代碼
  1. 'session'=>array(  

  2. 'class' => 'CDbHttpSession',  

  3. 'autoStart' => true,  

  4. 'sessionTableName'=>'YiiSession',  

  5. 'autoCreateSessionTable'=> false,  

  6. 'connectionID'=>'db',  

  7.        ),  

二,在Yii中使用cookie

Yii實現了一個cookie驗證機制,可以防止cookie被修改。啓用之後可以對cookie的值進行HMAC檢查。
Cookie驗證在默認情況下是禁用的。如果你要啓用它,可以編輯應用配置 中的組件中的CHttpRequest部分。

一定要使用經過Yii驗證過的cookie數據。使用Yii內置的cookies組件來進行cookie操作,不要使用$_COOKIES。

實例:

Php代碼  收藏代碼
  1. // 檢索一個名爲$name的cookie值

  2. $cookie=Yii::app()->request->cookies[$name];  

  3. $value=$cookie->value;  

  4. ......  

  5. // 設置一個cookie

  6. $cookie=new CHttpCookie($name,$value);  

  7. Yii::app()->request->cookies[$name]=$cookie;  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章