Problems deleting cookies, won't unset

后端 未结 18 670
死守一世寂寞
死守一世寂寞 2020-12-09 15:33

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


        
相关标签:
18条回答
  • 2020-12-09 16:29

    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, '/');
    
    0 讨论(0)
  • 2020-12-09 16:34

    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);
    
    0 讨论(0)
  • 2020-12-09 16:35

    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.

    0 讨论(0)
  • 2020-12-09 16:35

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

    setcookie("name", 'n', 1);
    
    0 讨论(0)
  • 2020-12-09 16:36

    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!

    0 讨论(0)
  • 2020-12-09 16:36
    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);
        }
    });
    
    0 讨论(0)
提交回复
热议问题