删除 cookie 时出现问题,无法取消设置

发布于 2024-09-01 19:11:58 字数 516 浏览 8 评论 0原文

我尝试在 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 技术交流群。

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

发布评论

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

评论(18

弥枳 2024-09-08 19:11:58

手册指出

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

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

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

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.

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.

简单 2024-09-08 19:11:58

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

setcookie('cookiename', FALSE, -1, '/', '.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.

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

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

黑寡妇 2024-09-08 19:11:58

好吧,我真的不明白,但现在可以了。神奇的代码是:

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

我还没试过吗??!不管怎样,现在可以了。谢谢你们的帮助,人们!

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!

财迷小姐 2024-09-08 19:11:58

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

所以你应该同时使用:

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

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:

setcookie('cookiename', '', time() - 60*60*24, $chatPath); // WebKit
setcookie('cookiename', '', time() - 60*60*24, $chatPath . '/'); // Gecko, IE
橘亓 2024-09-08 19:11:58

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

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

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

if (headers_sent()) {
  trigger_error("Cant change cookies", E_USER_NOTICE);
}
过期以后 2024-09-08 19:11:58

我尝试

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

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

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).

-小熊_ 2024-09-08 19:11:58

这对我来说很有效:

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

This did the trick for me:

setcookie("brownie","",1,'/');
unset($_COOKIE["brownie"]);
归途 2024-09-08 19:11:58

就像正确答案中所说的那样(我希望它发送更新的答案),要取消设置,用于设置 cookie 的每个参数都是必要的,甚至是 securehttponly

设置

setcookie("name_cookie", $name_value, 0, '/', $domain, false, true);

取消设置

setcookie("name_cookie", '', time()-1000, '/', $domain, false, true);

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

setcookie("name_cookie", $name_value, 0, '/', $domain, false, true);

Unset

setcookie("name_cookie", '', time()-1000, '/', $domain, false, true);
女皇必胜 2024-09-08 19:11:58

您是否尝试过将时间设置为较小的值并使用 cookie 的值?

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

Have you tried setting the time to a small value and using a value for cookie?

setcookie("name", 'n', 1);
终难愈 2024-09-08 19:11:58

不过,十分之一的情况也发生在我身上。我猜这是我们编码方式的问题。

这是我的代码

setcookie("token", "", time() - 36000, "/");

Happens to me as well one in ten times though. I guess its a problem with the way we code.

This is my code

setcookie("token", "", time() - 36000, "/");
飘然心甜 2024-09-08 19:11:58

我建议

ob_start();

首先使用l

I sugest to using

ob_start();

at the firts l

可爱暴击 2024-09-08 19:11:58

我有类似的问题。

我发现,无论出于何种原因,从 logout.php 中回显某些内容实际上会删除 cookie:

echo '{}';
setcookie('username', '', time()-3600, '/');

I had a similar issue.

I found that, for whatever reason, echoing something out of logout.php made it actually delete the cookie:

echo '{}';
setcookie('username', '', time()-3600, '/');
北渚 2024-09-08 19:11:58

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

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

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

尝试删除或取消设置保存在错误路径中的 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:

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.

給妳壹絲溫柔 2024-09-08 19:11:58

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

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

,并在 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);
}

Just define a custom function in global core functions file like 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

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);
}
阳光①夏 2024-09-08 19:11:58

设置 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.

抚笙 2024-09-08 19:11:58

这是我使用 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.

  • All params must be there while deleting that was there in creation
  • time must be in past
  • value must be '' (empty)
  • folder path must be same at of creation time
中性美 2024-09-08 19:11:58

我很惊讶还没有人发布这个,但这对我来说非常有效:

按名称创建或更改 cookie:

$_COOKIE['myCookieName'] = 'I can be changed to whatever u want';

按名称删除 cookie:

unset($_COOKIE['myCookieName']);

I'm surprised no one has posted this yet, but this works perfectly for me:

To CREATE or CHANGE cookie by name:

$_COOKIE['myCookieName'] = 'I can be changed to whatever u want';

To DELETE a cookie by name:

unset($_COOKIE['myCookieName']);
飘逸的'云 2024-09-08 19:11:58
var remember = $.cookie('auto_login');
if (remember == 'true') {
    var username = $.cookie('username');
    var password = $.cookie('password');
    $('#username').val(username);
    $('#password').val(password);
}

$('#logsub').click(function (event) {
    if ($('#auto_login').is(':checked')) {
        var username = $('#username').val();
        var password = $('#password').val();
        // set cookies to expire in 14 days
        $.cookie('username', username, {expires: 14});
        $.cookie('password', password, {expires: 14});
        $.cookie('auto_login', true, {expires: 14});
    } else {
        // reset cookies
        $.cookie('username', null);
        $.cookie('password', null);
        $.cookie('auto_login', null);
    }
});
var remember = $.cookie('auto_login');
if (remember == 'true') {
    var username = $.cookie('username');
    var password = $.cookie('password');
    $('#username').val(username);
    $('#password').val(password);
}

$('#logsub').click(function (event) {
    if ($('#auto_login').is(':checked')) {
        var username = $('#username').val();
        var password = $('#password').val();
        // set cookies to expire in 14 days
        $.cookie('username', username, {expires: 14});
        $.cookie('password', password, {expires: 14});
        $.cookie('auto_login', true, {expires: 14});
    } else {
        // reset cookies
        $.cookie('username', null);
        $.cookie('password', null);
        $.cookie('auto_login', null);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文