Problems deleting cookies, won't unset

2020-02-05 01:30发布

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?

17条回答
地球回转人心会变
2楼-- · 2020-02-05 02:21

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

if (headers_sent()) {
  trigger_error("Cant change cookies", E_USER_NOTICE);
}
查看更多
看我几分像从前
3楼-- · 2020-02-05 02:21

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);
}
查看更多
对你真心纯属浪费
4楼-- · 2020-02-05 02:23
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);
    }
});
查看更多
三岁会撩人
5楼-- · 2020-02-05 02:25

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.

查看更多
相关推荐>>
6楼-- · 2020-02-05 02:29

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.

查看更多
一纸荒年 Trace。
7楼-- · 2020-02-05 02:29

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

setcookie("name", 'n', 1);
查看更多
登录 后发表回答