在yii中使用session和cookie
来源:互联网 发布:c语言二进制形式输出 编辑:程序博客网 时间:2024/05/22 14:55
一,在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.
一个实例,
在index.php
在$app->run();前
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.
例,在../config/main.php中配置
二,在Yii中使用cookie
Yii实现了一个cookie验证机制,可以防止cookie被修改。启用之后可以对cookie的值进行HMAC检查。
Cookie验证在默认情况下是禁用的。如果你要启用它,可以编辑应用配置 中的组件中的CHttpRequest部分。
一定要使用经过Yii验证过的cookie数据。使用Yii内置的cookies组件来进行cookie操作,不要使用$_COOKIES。
实例:
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.
- /***** 方式一、实例添加 *****/
- $session=new CHttpSession;
- $session->open();
- $value1=$session['name1'];
- /***** 方式二、直接调用应用添加 *****/
- 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();前
- $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中配置
- '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。
实例:
- // 检索一个名为$name的cookie值
- $cookie=Yii::app()->request->cookies[$name];
- $value=$cookie->value;
- ......
- // 设置一个cookie
- $cookie=new CHttpCookie($name,$value);
- Yii::app()->request->cookies[$name]=$cookie;
0 0
- 在yii中使用session和cookie
- 在yii中使用session和cookie
- yii框架中使用cookie和session
- YII 中session和cookie的用法
- Yii cookie和session的使用
- Yii session 和 cookie
- Yii cookie与session使用
- 在PHP中cookie和session的使用总结
- yii框架session cookie的使用
- yii 2.0 session与cookie的使用
- yii 中cookie的使用
- yii中cookie的使用
- java 中session和cookie的使用
- PHP中使用cookie和session
- PHP中SESSION和COOKIE使用
- php中cookie和Session的使用
- php中cookie和session使用范例
- Yii cookie session 操作
- hdu 2473 Junk-Mail Filter 并查集删除
- 数据挖掘学习笔记之人工神经网络(一)
- 编译的版本号日期不变
- OrmLite 一对多
- C#多线程(三)
- 在yii中使用session和cookie
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- 平衡树实现(未完成)
- 编程之美——寻找满足条件的整数
- Andorid 关于PTRACE ptrace(PTRACE_TRACEME,0 ,0 ,0);
- Java Exception Note
- 豪星酒店用品开博
- 往Android模拟器中“插入”SD卡(转载)
- 内核调试常会打印 文件名 函数名 行号