//首先新建cookie
$cookie = new CHttpCookie(‘mycookie’, ‘this is my cookie’);
//定義cookie的有效期
$cookie->expire = time()+60*60*24*30; //有限期30天
//把cookie寫(xiě)入cookies使其生效
Yii::app()->request->cookies['mycookie']=$cookie;
注意:CHttpCookie定義的cookie名”mycookie”與Yii::app()->request->cookies['mycookie']中的”mycookie”必須一致,即每次添加cookie值都要寫(xiě)兩次cookie名
讀取cookie:
$cookie = Yii::app()->request->getCookies();
echo $cookie['mycookie']->value;
銷毀cookie:
$cookie = Yii::app()->request->getCookies();
unset($cookie[$name]);
設(shè)置session變量:
Yii::app()->session['var']=’value’;
使用: echo Yii::app()->session['var'];
移除: unset(Yii::app()->session['var']);
更為復(fù)雜一點(diǎn)的使用時(shí)如何配置你的session
配置項(xiàng)可設(shè)在 protected/config/main.php的components中:
‘session’=>array(
‘a(chǎn)utoStart’=>false(/true),
‘sessionName’=>’Site Access’,
‘cookieMode’=>’only’,
‘savePath’='/path/to/new/directory’,
),