设置cookie:
//首先新建cookie
$cookie = new CHttpCookie(‘mycookie’, ‘this is my cookie’);
//定义cookie的有效期
$cookie->expire = time()+60*60*24*30; //有限期30天
//把cookie写入cookies使其生效
Yii::app()->request->cookies['mycookie']=$cookie;
注意:CHttpCookie定义的cookie名”mycookie”与Yii::app()->request->cookies['mycookie']中的”mycookie”必须一致,即每次添加cookie值都要写两次cookie名
读取cookie:
$cookie = Yii::app()->request->getCookies();
echo $cookie['mycookie']->value;
销毁cookie:
$cookie = Yii::app()->request->getCookies();
unset($cookie[$name]);
设置session变量:
Yii::app()->session['var']=’value’;
使用: echo Yii::app()->session['var'];
移除: unset(Yii::app()->session['var']);
更为复杂一点的使用时如何配置你的session
配置项可设在 protected/config/main.php的components中:
‘session’=>array(
‘autoStart’=>false(/true),
‘sessionName’=>’Site Access’,
‘cookieMode’=>’only’,
‘savePath’='/path/to/new/directory’,
),