删除 cookie 时出现问题,无法取消设置
我尝试在 php 手册和互联网上搜索如何删除 cookie,并且按照他们所说的完全相同的方式进行了尝试:
setcookie("name", '', 1);
或者
setcookie("name", '', time()-3600);
但是当我在 Firefox 的 cookie 对话框中检查 cookie 时,它仍然存在,且具有相同的值。 我使用以下行设置此 cookie:
setcookie("name", $value, time() + 259200, $path);
我在 stackoverflow 上发现了这个问题: ,但没有一个答案解决了问题。我也尝试像作者说的那样把所有参数都放进去,但是没有效果。
有人看到问题所在吗?
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:
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:
setcookie("name", $value, time() + 259200, $path);
I found this question 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.
Does anyone see the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
手册指出:
因此,还要确保
$path
指定正确 - 删除时也如此。例如,如果 cookie 是在子目录中指定的,您可能无法从父目录或子目录(或两者)中删除它。我不完全确定权限是如何工作的,但您可能想要使用 Web 开发人员工具栏来查看您尝试删除的 cookie 的路径。
The manual states:
So also make sure that
$path
is 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).I'm not entirely sure how the permissions work, but you might want to use the Web Developer Toolbar to view what the path is of the cookie you are attempting to delete.
我很惊讶没有人提到它(或者也许我错过了),但是域名也很重要!如果您在 sub-domain.example.com 上,并且 cookie 来自 .example.com,那么您需要显式设置域参数,否则它将假定当前域并且无法工作。
子域值不会清除父域的 cookie。
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-domains value will not clear cookies from parent domain.
好吧,我真的不明白,但现在可以了。神奇的代码是:
我还没试过吗??!不管怎样,现在可以了。谢谢你们的帮助,人们!
Ok, I really don't understand, but it works now. The magic code is:
Haven't I already tried that??! Whatever, it works now. Thanks for your help, people!
如果您删除特定路径的cookie,并且您的path参数以尾部斜杠“/”结尾,那么它将在 Firefox 和 IE 中工作,但不适用于 Chrome 和 Opera。如果没有尾部斜杠,则它仅适用于 Chrome 和 Opera。
所以你应该同时使用:
If you delete cookie for the specific path and your path parameter 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.
So you should use both:
您是否检查过您的脚本是否已发送其 HTTP 标头?
Did you check if your script already send its HTTP headers?
我尝试
在我的服务器上使用 Apache/PHP5,它清除了 cookie(至少 var_dump($_COOKIE) 显示一个空数组)。
I tried using
and on my server with Apache/PHP5 it cleared the cookie (at least a var_dump($_COOKIE) showed an empty array).
这对我来说很有效:
This did the trick for me:
就像正确答案中所说的那样(我希望它发送更新的答案),要取消设置,用于设置 cookie 的每个参数都是必要的,甚至是 secure 和 httponly
设置
取消设置
Just like is said in the correct answer (I want it to send an updated one), to unset, every parameter used to set the cookie is necessary, even secure and httponly
Set
Unset
您是否尝试过将时间设置为较小的值并使用 cookie 的值?
Have you tried setting the time to a small value and using a value for cookie?
不过,十分之一的情况也发生在我身上。我猜这是我们编码方式的问题。
这是我的代码
Happens to me as well one in ten times though. I guess its a problem with the way we code.
This is my code
我建议
首先使用l
I sugest to using
at the firts l
我有类似的问题。
我发现,无论出于何种原因,从 logout.php 中回显某些内容实际上会删除 cookie:
I had a similar issue.
I found that, for whatever reason, echoing something out of logout.php made it actually delete the cookie:
有时,您将 cookie 保存在与尝试删除/使用它的路径不同的路径中。
进入例如。 Chrome cookie 设置并检查 cookie 路径,然后将路径添加到 setcookie 命令,然后像这样删除它:
尝试删除或取消设置保存在错误路径中的 cookie 将不起作用,并且可能会非常令人沮丧。
Sometimes you saved the cookie in a different path than you're trying to delete/uset it in.
Go into eg. Chrome cookie settings and check the cookie path, then add the path to the setcookie command, and delete it like this:
Trying to delete or unset a cookie that is saved in the wrong path will not work and can be very frustrating.
只需在全局核心函数文件(如 global.php)中定义一个自定义函数
,并在 html 代码顶部使用此函数,如
Just define a custom function in global core functions file like global.php
and use this function at the top of the html code like
设置 cookie
setcookie('cookiename', $cookie_value, time() + (86400 * 30), "/");
// 86400 = 1 天
取消设置 cookie
setcookie('cookiename', '', time() - 3600, "/");
无需惊慌。只需复制您用来设置 cookie 的函数,然后减去时间即可。不要混淆,使其简单明了。
set a cookie
setcookie('cookiename', $cookie_value, time() + (86400 * 30), "/");
// 86400 = 1 day
unset cookie
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 的经验,在浏览器窗口(我们用来查看现有 cookie)关闭之前,cookie 可能不会从客户端计算机中删除。因此,关闭该窗口并尝试您的代码。
this is my experience with cookie that, the cookie may not be deleted from the client machine until the browser window (that we use to see existing cookie) is closed. So close that window and the try your code.
我很惊讶还没有人发布这个,但这对我来说非常有效:
按名称创建或更改 cookie:
按名称删除 cookie:
I'm surprised no one has posted this yet, but this works perfectly for me:
To CREATE or CHANGE cookie by name:
To DELETE a cookie by name: