【发布时间】:2015-03-20 10:41:56
【问题描述】:
我一直在测试 cookie 创建、修改和删除,遇到了一个我似乎无法解释的“怪癖”。
我可以正常创建一个 30 天有效期的 cookie:
setcookie("test_cookie", "test_value", time() + (86400 * 30), "/");
然后我可以轻松地“删除”那个 cookie:
setcookie("test_cookie", "", time() - 3600, "/");
但是,当我创建一个过期时间为0 的 cookie(即,将在浏览器关闭/会话结束时过期)时,怪癖就来了:
setcookie("test_cookie", "test_value", 0, "/");
我现在无法在不关闭浏览器的情况下手动删除该 cookie!将其到期时间设置为过去的时间没有任何作用。什么给了?
我一直用于测试的整个页面,注释掉我不使用的行:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Cookie Test</title>
</head>
<body>
<?php
// setcookie("test_cookie", "test_value", time() + (86400 * 30), "/");
// setcookie("test_cookie", "test_value_updated", time() + (86400 * 30), "/");
// setcookie("test_cookie", "", time() - 3600, "/");
// setcookie("test_cookie", "test_value", 0, "/");
// setcookie("test_cookie", "test_value_updated", 0, "/");
setcookie("test_cookie", "", time() - 3600, "/");
print_r($_COOKIE);
?>
</body>
</html>
【问题讨论】: