简体   繁体   中英

Cookie won't delete (php)

I'm trying to delete all cookies from a domain/path with this code :

if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        unset($_COOKIE[''.$name.'']);
        setcookie($name, '', time()-1000);
        setcookie($name, '', time()-1000, '/');
    }
}

But it delete only some cookies and others... I can't understant why... Any help please? Thanks

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.

Make sure that you have the correct cookie name , and also make sure you've got the right path for the cookies`. 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).

You might want to use the Web Developer Toolbar to view what the path is of the cookie you are attempting to delete.

If you can provided bit more of an example of what you have so far, then I can update my answer with an example on what you can do with your code.

Found it, the domain (which was not specified). In addition there is a . before domain, appears to be important too...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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