Suhail Gupta
Suhail Gupta

Reputation: 23276

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?

Upvotes: 1

Views: 44

Answers (2)

Abhishek Sharma
Abhishek Sharma

Reputation: 6661

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

Upvotes: 1

jeroen
jeroen

Reputation: 91742

$_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),'/');

Upvotes: 4

Related Questions