Yii cookie 的使用方法

设置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]);
若您觉得我的博文对您有帮助,欢迎点击下方按钮对我打赏
打赏