我的cookie是这样设置的:
$cookie = new CHttpCookie('username','Jack'); $cookie->expire = time()+60*60*24*30; //有限期30天 $cookie->domain = '.domain.com'; $cookie->path = '/'; Yii::app()->request->cookies['username'] = $cookie;
设置完之后能在 *.domain.com 访问到这个cookie
访问方式为:
$cookie = Yii::app()->request->cookies['official_username']; echo $cookie->value;
然而,我用这种方式清除掉这个cookie,发现并没有效果
$cookie = Yii::app()->request->getCookies(); if ($cookie['username']) {
unset($cookie['username']);
}
这样操作反而无法将这个cookie删掉,还是能正常访问到
包括修改了cookie的value为空,设置过期时间为-1都无法把这个cookie删掉
折腾了很久之后
找到了解决方法
$cookie = new CHttpCookie('username',''); $cookie->expire = -1; //失效 $cookie->domain = '.domain.com'; $cookie->path = '/'; Yii::app()->request->cookies['username'] = $cookie;
就这样 重新给这个cookie赋值就可以解决cookie无法删除的问题