php 删除 cookie 时出现问题,不会取消设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2856366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:55:53  来源:igfitidea点击:

Problems deleting cookies, won't unset

phpcookiesunset

提问by RemiX

I've tried searching the php manual and internet on how to delete cookies and I've tried it the exact same way they all say:

我已经尝试在 php 手册和互联网上搜索有关如何删除 cookie 的信息,并且我已经按照他们所说的完全相同的方式进行了尝试:

setcookie("name", '', 1);

or

或者

setcookie("name", '', time()-3600);

But when I check the cookies in the cookies dialog in Firefox, it's still there with the same value. I set this cookie using the following line:

但是当我在 Firefox 的 cookie 对话框中检查 cookie 时,它​​仍然具有相同的值。我使用以下行设置此 cookie:

setcookie("name", $value, time() + 259200, $path);

I found thisquestion on stackoverflow: , but none of the answers solved the problem. I also tried putting all paramaters in, like the author said, but it had no effect.

我在 stackoverflow: 上发现了这个问题,但没有一个答案解决了这个问题。我也试过把所有参数都放进去,就像作者说的那样,但没有效果。

Does anyone see the problem?

有没有人看到问题?

回答by Paul Lammertsma

The manual states:

该手册指出

Cookies must be deleted with the same parameters as they were set with. If the value argument is an empty string, or FALSE, and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the remote client. This is internally achieved by setting value to 'deleted' and expiration time to one year in past.

必须使用与设置时相同的参数删除 Cookie。如果 value 参数是一个空字符串,或者FALSE,并且所有其他参数都与之前对 setcookie 的调用匹配,那么具有指定名称的 cookie 将从远程客户端中删除。这是通过将值设置为“已删除”并将过期时间设置为过去一年来在内部实现的。

So also make sure that $pathis specified correctly -- also when deleting it. For instance, if the cookie was specified in a subdirectory, you may not be able to delete it from either the parent or children directories (or both).

因此,还要确保$path正确指定 -删除它时也是如此。例如,如果 cookie 是在子目录中指定的,您可能无法从父目录或子目录(或两者)中删除它。

I'm not entirely sure how the permissions work, but you might want to use the Web Developer Toolbar to view what the pathis of the cookie you are attempting to delete.

我不完全确定权限是如何工作的,但您可能希望使用 Web 开发人员工具栏查看您尝试删除的 cookie的路径

回答by RemiX

Ok, I really don't understand, but it works now. The magic code is:

好吧,我真的不明白,但它现在有效。神奇的代码是:

setcookie("name", '', 1, $path);

Haven't I already tried that??! Whatever, it works now. Thanks for your help, people!

我不是已经试过了吗??!无论如何,它现在有效。感谢您的帮助,人们!

回答by bfuzze

I'm surprised no one has mentioned it (or maybe I missed it), but domain is important too! If you are on sub-domain.example.com, and the cookie is from .example.com, then you need to explicitly set the domain parameter, otherwise it will assume the current domain and it won't work.

我很惊讶没有人提到它(或者我可能错过了它),但是域也很重要!如果您在 sub-domain.example.com 上,并且 cookie 来自 .example.com,那么您需要显式设置 domain 参数,否则它将假定为当前域并且不起作用。

setcookie('cookiename', FALSE, -1, '/', '.example.com');

Sub-domains value will not clear cookies from parent domain.

子域值不会从父域中清除 cookie。

回答by Ruslan

If you deletecookie for the specific path and your pathparameter ends with the trailing slash '/'then it will work in Firefox and IE, but won't work in Chrome and Opera. If there is no trailing slash then it will only work in Chrome and Opera.

如果您删除特定路径的 cookie 并且您的路径参数以尾部斜杠“/”结尾,那么它将在 Firefox 和 IE 中工作,但在 Chrome 和 Opera 中不起作用。如果没有尾部斜杠,那么它只能在 Chrome 和 Opera 中工作。

So you should use both:

所以你应该同时使用两者:

setcookie('cookiename', '', time() - 60*60*24, $chatPath); // WebKit
setcookie('cookiename', '', time() - 60*60*24, $chatPath . '/'); // Gecko, IE

回答by Keeper

I tried using

我尝试使用

setcookie("name", "", -1);

and on my server with Apache/PHP5 it cleared the cookie (at least a var_dump($_COOKIE) showed an empty array).

在我的 Apache/PHP5 服务器上,它清除了 cookie(至少一个 var_dump($_COOKIE) 显示了一个空数组)。

回答by Bob Fanger

Did you check if your script already send its HTTP headers?

您是否检查过您的脚本是否已经发送了其 HTTP 标头?

if (headers_sent()) {
  trigger_error("Cant change cookies", E_USER_NOTICE);
}

回答by Rajnesh Thakur

set a cookie

设置一个cookie

setcookie('cookiename', $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

setcookie('cookiename', $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 天

unset cookie

取消设置 cookie

setcookie('cookiename', '', time() - 3600, "/");

setcookie('cookiename', '', time() - 3600, "/");

No need to panic. Just copy function you use to set cookie and now minus the time. Do not get confuse, make it easy and clear.

没有必要恐慌。只需复制您用来设置 cookie 的功能,现在减去时间。不要混淆,让它简单明了。

回答by Felipe

This did the trick for me:

这对我有用:

setcookie("brownie","",1,'/');
unset($_COOKIE["brownie"]);

回答by kochauf

Sometimes you saved the cookie in a different path than you're trying to delete/uset it in.

有时,您将 cookie 保存在与尝试删除/使用它不同的路径中。

Go into eg. Chrome cookie settings and check the cookie path, then add the path to the setcookie command, and delete it like this:

进入例如。Chrome cookie 设置并检查 cookie 路径,然后将路径添加到 setcookie 命令,并像这样删除它:

setcookie( "my_cookie_name","",1,'/mypath');

Trying to delete or unset a cookie that is saved in the wrong path will not work and can be very frustrating.

尝试删除或取消设置保存在错误路径中的 cookie 是行不通的,而且会非常令人沮丧。

回答by karthikeyan ganesan

Just define a custom function in global core functions file like global.php

只需在 global.php 等全局核心函数文件中定义一个自定义函数

function delete_cookie()
{
unset($_COOKIE['cookiename']);
setcookie('cookiename',NULL,time()-3600, '/');
return true;
}

and use this function at the top of the html code like

并在 html 代码的顶部使用此功能,例如

include('global.php')
if(isset($_GET['delete_cookie']))
{
delete_cookie(); //if you want to pass the parameters into the function also possible like delete_cookie(param1);
}