无法删除Cookie,它仍然显示在其他文件中调用它

时间:2018-10-18 22:35:50

标签: php cookies

创建cookie

$cookie_name = "userw";
$cookie_value = "John due";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}

删除cookie

// set the expiration date to one hour ago
$cookie_name = "userw";

setcookie("$cookie_name", "", time() - 3600);
unset($_COOKIE[$cookie_name]);


$cookie_name = "userw";
if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}

删除后读取cookie

$cookie_name = "userw";
if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}

如果在未设置的情况下读取文件中的cookie,则cookie不会出现。但是当我再次读取Cookie时,它就会显示出来。我在xampp中测试了它。 有人对此有任何解决方案吗?

1 个答案:

答案 0 :(得分:1)

删除Cookie时,您需要指定创建时指定的相同路径(除非您想做非常具体的事情):

尝试一下:

setcookie($cookie_name, '', time() - 3600, '/');   // note the 4th param, '/'