mauzilla
mauzilla

Reputation: 3592

Unable to delete cookies

I am using cookies as a remember me function on my site. My method creates a number of cookies which works like a charm, however, I am unable to delete or change these values afterwards.

I create them by:

private function setCookies($userInfo) {
    setcookie("profileID",$userInfo["profileID"],time()+60*60*24*30);
    setcookie("userLevel",$userInfo["levelID"],time()+60*60*24*30);
    setcookie("username",$userInfo["username"],time()+60*60*24*30);
    setcookie("fullname",$userInfo["fullname"],time()+60*60*24*30);
    return true;
}

and my logout method:

public function logout() {
    session_destroy();
    setcookie("profileID","",time()-60*60*24*30*12);
    setcookie("userLevel","",time()-60*60*24*30*12);
    setcookie("username","",time()-60*60*24*30*12);
    setcookie("fullname","",time()-60*60*24*30*12);
    return true;
}

Both these methods are called within a controller file, before any output to the browser (head ect). What makes things worse is that it does not even change the values as list above to "" but remains with the original values.

Any ideas? I have not worked with cookies that much so this could be a simple error :)

Upvotes: 0

Views: 891

Answers (3)

rjv
rjv

Reputation: 6766

Use session_unset() before the session_destroy() in the logout method

Upvotes: 0

evasilchenko
evasilchenko

Reputation: 1870

Please read the php manual for how to use session_destroy. It talks about cookies and how to destroy them.

http://php.net/manual/en/function.session-destroy.php

In order to use the same cookies again, you will need to call session_start() prior to doing so. You're calling session_destroy() and then trying to set your cookies.

Upvotes: 1

Mathieu Dumoulin
Mathieu Dumoulin

Reputation: 12244

Cookies updated to a negative time or 0 time will get deleted only when the browser restarts if i'm not mistaken. Cookie expiration is managed through the browser's cleanup method that runs when the browser is closed.

Upvotes: 0

Related Questions