简体   繁体   English

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

[英]Problems deleting cookies, won't unset

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.但是当我在 Firefox 的 cookie 对话框中检查 cookie 时,它​​仍然具有相同的值。 I set this cookie using the following line:我使用以下行设置此 cookie:

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

I found this question on stackoverflow: , but none of the answers solved the problem.我在 stackoverflow: 上发现了 这个问题,但没有一个答案解决了这个问题。 I also tried putting all paramaters in, like the author said, but it had no effect.我也试过把所有参数都放进去,就像作者说的那样,但没有效果。

Does anyone see the problem?有没有人看到问题?

The manual states :该手册指出

Cookies must be deleted with the same parameters as they were set with.必须使用与设置时相同的参数删除 Cookie。 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.如果 value 参数是空字符串或FALSE ,并且所有其他参数都与先前对 setcookie 的调用匹配,则将从远程客户端删除具有指定名称的 cookie。 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 .因此还要确保正确指定了$path -删除它时也是如此 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).例如,如果 cookie 是在子目录中指定的,您可能无法从父目录或子目录(或两者)中删除它。

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

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!感谢您的帮助,人们!

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。

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.如果您删除特定路径的 cookie 并且您的路径参数以尾部斜杠“/”结尾,那么它将在 Firefox 和 IE 中工作,但在 Chrome 和 Opera 中不起作用。 If there is no trailing slash then it will only work in Chrome and 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

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) 显示了一个空数组)。

Did you check if your script already send its HTTP headers?您是否检查过您的脚本是否已经发送了其 HTTP 标头?

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

This did the trick for me:这对我有用:

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

I sugest to using我建议使用

ob_start();

at the firts l一开始我

I had a similar issue.我有一个类似的问题。

I found that, for whatever reason, echoing something out of logout.php made it actually delete the cookie:我发现,无论出于何种原因,从 logout.php 中回显某些内容实际上会删除 cookie:

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

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

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

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, "/");

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 是行不通的,而且会非常令人沮丧。

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);
}

set a cookie设置一个cookie

setcookie('cookiename', $cookie_value, time() + (86400 * 30), "/"); setcookie('cookiename', $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day // 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.只需复制您用来设置 cookie 的功能,现在减去时间。 Do not get confuse, make it easy and clear.不要混淆,让它简单明了。

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就像在正确答案中所说的一样(我希望它发送一个更新的),要取消设置,用于设置 cookie 的每个参数都是必要的,即使是安全httponly

Set

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

Unset取消设置

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

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.这是我对 cookie 的经验,在浏览器窗口(我们用来查看现有 cookie)关闭之前,可能不会从客户端计算机中删除 cookie。 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创建时文件夹路径必须相同

I'm surprised no one has posted this yet, but this works perfectly for me:我很惊讶还没有人发布这个,但这对我来说非常有效:

To CREATE or CHANGE cookie by name:要按名称创建或更改 cookie:

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

To DELETE a cookie by name:要按名称删除 cookie:

unset($_COOKIE['myCookieName']);
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);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM