简体   繁体   中英

Why aren't the cookies deleted?

I am trying to unset the cookies, I had earlier set as:

    setcookie(session_name(),$sessionID,time() + 30*24*3600,'/');
    setcookie('UserID',$result[0]['UserID'],time() + 30*24*3600,'/');
    setcookie('UType',$result[0]['UType'],time() + 30*24*3600,'/');
    setcookie('Username',$Username,time() + 30*24*3600,'/');

Logout File:

function unsetCookie() {
    foreach($_COOKIE as $key => $value) {
        // $_COOKIE[$key] contains the cookie name as expected
        setcookie($_COOKIE[$key],'',time()-(40*24*3600),'/');
    }
}

unsetCookie();
session_start();
session_destroy();
header('Location: '.$loginPage);
exit();

But after the redirect in the logout file, cookies are still not deleted. What could be the reason for this?

$_COOKIE[$key] contains the value of your cookie, not the key as that is $key .

So you would need:

setcookie($key,'',time()-(40*24*3600),'/');

Set the value to "" and the expiry date to yesterday (or any date in the past)

Try this code like that :-

setcookie("UserID", "", time()-(40*24*3600));
setcookie("UType", "", time()-(40*24*3600));
setcookie("Username", "", time()-(40*24*3600));

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