Problems deleting cookies, won't unset

后端 未结 18 668
死守一世寂寞
死守一世寂寞 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:13

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

    if (headers_sent()) {
      trigger_error("Cant change cookies", E_USER_NOTICE);
    }
    
    0 讨论(0)
  • 2020-12-09 16:15

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

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

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

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

    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.

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

    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
    
    0 讨论(0)
提交回复
热议问题