简体   繁体   中英

Why this setcookie doesn't work?

setcookie("cookie1", "", 0, "/",".domain.com");
setcookie("cookie2", "", 0, "/",".domain.com");
header('Location: /index.php');

It doesn't delete cookie1 and cookie2. Why is that?

An expiration time of 0 is a special value which means the cookie will be deleted when the browser is closed. To delete it immediately, you need to give a valid expiration time in the past. An example from the PHP docs :

// set the expiration date to one hour ago
setcookie ("TestCookie", "", time() - 3600);

My best guess would be that the browser simply hasn't done it yet. Cookie management is the responsibility of the browser, and depending on the browser settings it may not delete your cookie immediately.

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