无法删除php set cookie

goi*_*ing 7 php cookies

我在php中通过这个调用设置了一个cookie

setcookie('alert_msg', 'you have the add badge');
Run Code Online (Sandbox Code Playgroud)

我试过用这种方式解开它

setcookie('alert_msg', '');
setcookie('alert_msg', false);
setcookie('alert_msg', false, 1);
setcookie('alert_msg', false, time()-3600);
setcookie('alert_msg', '', 1, '/');
Run Code Online (Sandbox Code Playgroud)

并且它仍然不会取消$ _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, '/');
}
Run Code Online (Sandbox Code Playgroud)

Sha*_*ngh 21

检查cookie路径.

由于您没有将path参数传递给setcookie函数,因此在这种情况下,cookie将仅为当前目录设置并且可以使用,并且只能从该目录中取消设置.

可能的解决方案是将path值传递为/.因此,可以在应用程序的任何部分使用和取消设置cookie.