简体   繁体   中英

Proper expire value to delete a cookie

Per http://php.net/manual/en/function.setcookie.php , they provide the following example to delete a cookie:

setcookie ("TestCookie", "", time() - 3600);

Selected answer to Remove a cookie recommends the following:

setcookie('Hello', null, -1, '/');

Should it be time()-3600 , -1 , or something else?

On a side note, is a value of null or "" preferred?

Try this

if (isset($_COOKIE['TestCookie'])) 
{
    // removing the cookie
    unset($_COOKIE['TestCookie']);

    // resetting the cookie
    setcookie('TestCookie', null, -1, '/');

    return true;
} else {
    return false;
}

Since cookie expiration time will be checked against clients clock, the best option is:

setcookie('Hello', null, 1, '/');

Then you can make sure it will expire instantly.

Except if the clock is 00:00:00 1970-01-01 :P

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