在yii中使用session和cookie

一,在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. /***** 方式一、实例添加 *****/  
  2. $session=new CHttpSession;  
  3. $session->open();  
  4. $value1=$session['name1'];  
  5.   
  6. /***** 方式二、直接调用应用添加 *****/  
  7. Yii::app()->session->add('name','foobar');  
  8. Yii::app()->session->add('name2','foobar');  
  9. Yii::app()->session->add('name3','foobar');  
  10. //或者  
  11. $session = Yii::app()->session;  
  12. $session['key'] = 'value';  
  13. var_dump($session['key']);  
  14.    
  15. //遍历  
  16. foreach($session as $name=>$value)  


一个实例, 
Php代码   收藏代码
  1. $session = new CHttpSession;  
  2. $session->open();  
  3.           
  4. $user_id = $this->user->id;  
  5. $sessionKey = $user_id.'_is_sending';  
  6.           
  7. if(isset($session[$sessionKey])){  
  8.     $first_submit_time = $session[$sessionKey];  
  9.     $current_time      = time();  
  10.     if($current_time - $first_submit_time < 10){  
  11.         $session[$sessionKey] = $current_time;  
  12.         $this->response(array('status'=>1, 'msg'=>'不能在10秒钟内连续发送两次。'));  
  13.     }else{  
  14.         unset($session[$sessionKey]);//超过限制时间,释放session";  
  15.     }  
  16. }  
  17.   
  18. //第一次点击确认按钮时执行  
  19. if(!isset($session[$sessionKey])){  
  20.     $session[$sessionKey] = time();  
  21. }  
  22.           
  23. var_dump($sessionKey);var_dump($session[$sessionKey]);exit();  


在index.php 
在$app->run();前 
Php代码   收藏代码
  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. CREATE TABLE `YiiSession` (  
  2.   `id` char(32) NOT NULL,  
  3.   `expire` int(11) default NULL,  
  4.   `data` text,  
  5.   PRIMARY KEY  (`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;  
基于STM32F407,使用DFS算法实现最短迷宫路径检索,分为三种模式:1.DEBUG模式,2. 训练模式,3. 主程序模式 ,DEBUG模式主要分析bug,测量必要数据,训练模式用于DFS算法训练最短路径,并将最短路径以链表形式存储Flash, 主程序模式从Flash….zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计,皆可应用在项目、毕业设计、课程设计、期末/期/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值