【发布时间】:2011-09-05 10:33:01
【问题描述】:
我在 php 中通过这个调用设置了一个 cookie
setcookie('alert_msg', 'you have the add badge');
我试过用这种方式取消设置
setcookie('alert_msg', '');
setcookie('alert_msg', false);
setcookie('alert_msg', false, 1);
setcookie('alert_msg', false, time()-3600);
setcookie('alert_msg', '', 1, '/');
它仍然不会取消设置 $_COOKIE['alert_msg'] 中的 cookie 值。
Firefox 和 Chrome 都试过了
代码示例:
if (isset($_COOKIE['alert_msg'])) {
$this->set('alert_msg', $_COOKIE['alert_msg']);
unset($_COOKIE['alert_msg']);
setcookie('alert_msg', '', 1, '/');
}
【问题讨论】:
-
检查 cookie 路径。您是否在
dir/file中设置cookie 并尝试在dir2/file中取消设置? -
@Shakti - 我正在使用 Cakephp。我将它设置在 /app/controllers 中的一个控制器中,并尝试在 /app 中的 app_controller 中取消设置,这有什么不同吗?
-
@Shakti - 我看你是对的,能否请您添加您的评论作为答案,我可以将其标记为我接受的答案。
-
cookie 名称中的点被替换为 _ 字符 - 这可能会导致一些问题
-
您可能会发现
$cookie->delete()很有帮助,如在 this standalone library 中找到的那样。