如何删除cookie?

发布于 2024-12-12 07:15:09 字数 564 浏览 0 评论 0原文

当我 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

み格子的夏天 2024-12-19 07:15:09

您确定使用与创建 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, call unset($_COOKIE['name']).

简美 2024-12-19 07:15:09

尝试:

unset($_COOKIE['filters']['input1']);
unset($_COOKIE['filters']['input2']);
$time = time() + 1000; // enything you want, if its in the past $_COOKIE['filters'] will no loger exist
setcookie('filters', $_COOKIE['filters'], $time, '/' );

try:

unset($_COOKIE['filters']['input1']);
unset($_COOKIE['filters']['input2']);
$time = time() + 1000; // enything you want, if its in the past $_COOKIE['filters'] will no loger exist
setcookie('filters', $_COOKIE['filters'], $time, '/' );
烏雲後面有陽光 2024-12-19 07:15:09

您必须在运行 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

诠释孤独 2024-12-19 07:15:09

如果您使用域创建 cookie,则可能需要使用相同的域名来删除。

设置 cookie:

setcookie('mycookie', 'value', time() + 999, '/', '.my.domain', false);

删除 cookie:

 setcookie('mycookie', "", -1, '/', '.my.domain', false);

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:

setcookie('mycookie', 'value', time() + 999, '/', '.my.domain', false);

to delete cookie:

 setcookie('mycookie', "", -1, '/', '.my.domain', false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文