如何删除cookie?
当我 print_r($_COOKIE);
时,我得到以下结果。
Array ( [filters] => Array ( [input1] => 1 [input2] => 20000 [input3] => none ) [PHPSESSID] => 12334 )
我想删除一些元素并希望它像这样:
Array ( [filters] => Array ( [input3] => none ) [PHPSESSID] => 12334 )
我尝试了这个,但它没有影响 $_COOKIE
中的任何内容
$past = time() - 3600;
setcookie( "filters[input1]", "", $past, '/' );
setcookie( "filters[input2]", "", $past, '/' );
这里出了什么问题?整天都在尝试这个吗?
谢谢
When I print_r($_COOKIE);
, I get following result.
Array ( [filters] => Array ( [input1] => 1 [input2] => 20000 [input3] => none ) [PHPSESSID] => 12334 )
I want to delete some element and want it to be like this:
Array ( [filters] => Array ( [input3] => none ) [PHPSESSID] => 12334 )
I tried this but it not effecting anything in $_COOKIE
$past = time() - 3600;
setcookie( "filters[input1]", "", $past, '/' );
setcookie( "filters[input2]", "", $past, '/' );
What is wrong here? All day is waisted trying this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您确定使用与创建 cookie 相同的参数(路径、安全等)调用
setcookie()
来删除 cookie 吗?此外,
setcookie()
不会影响正在运行的脚本中的$_COOKIE
。只有对该脚本的后续调用才会具有修改后的$_COOKIE
数组。要在同一会话中从$_COOKIE
中删除值,请调用unset($_COOKIE['name'])
。Are you sure that you call
setcookie()
to delete the cookie with the same arguments (path, secure, etc) as you did to create the cookie?Also,
setcookie()
does not affect$_COOKIE
in the running script. Only subsequent calls to that script will have the modified$_COOKIE
array. To remove values from$_COOKIE
in the same session, callunset($_COOKIE['name'])
.尝试:
try:
您必须在运行 setcookie() 后重新加载页面 - 在重新加载页面之前,设置 cookie 的结果不可用。还可以尝试使用“较早的”时间 - 超过 1 小时 - 尝试使用一年前的时间。如果时间还不够远,有些浏览器不会删除 cookie
You must reload the page after your run setcookie() - the result of your setting cookie is not available until you reload the page. Also try using 'older' time - older than 1 hour - try like a year in the past. Some browsers will not delete cookie if the time is not far enough in the past
如果您使用域创建 cookie,则可能需要使用相同的域名来删除。
即
设置 cookie:
删除 cookie:
If you created your cookies with a domain , you may need to remove then using the same domain name.
i.e.
to set a cookie:
to delete cookie: