Page 1 of 1

How do you kill cookies once and for all??

Posted: Wed Jan 08, 2014 2:46 pm
by omarel
This code does not kill the cookie. I verified this by trying to display the cookie afterward nd it still displays:

Code: Select all

<?php
// set the expiration date to one hour ago
if(isset($_COOKIE['name'])) {
  unset($_COOKIE['name']);
  setcookie('name', '', time() - 3600); // empty value and old timestamp
}
?> 

I test the cookie with this (on another page) and it still displays:

Code: Select all

<?php
if (isset($_COOKIE["name"]))
{ echo 'it exists';}
?>

Re: How do you kill cookies once and for all??

Posted: Wed Jan 08, 2014 3:34 pm
by social_experiment
yes it seems you have to close the browser instance to kill the cookies' persistance

Re: How do you kill cookies once and for all??

Posted: Wed Jan 08, 2014 9:35 pm
by omarel
Closing the browser doesnt work either

Re: How do you kill cookies once and for all??

Posted: Wed Jan 08, 2014 10:41 pm
by requinix
Are you sure you're clearing (trying to) before you've outputted anything? Have you matched the cookie parameters - domain and path?

Re: How do you kill cookies once and for all??

Posted: Thu Jan 09, 2014 5:39 am
by omarel
My code above is how I'm trying to clear the cookie. Do I have to include the same parameters when unsetting as I did when setting?

Re: How do you kill cookies once and for all??

Posted: Thu Jan 09, 2014 7:35 am
by HeyThere
omarel: I mean that what @social_experiment wanted to say is that you should check if your headers were not already sent (http://www.php.net/manual/en/function.headers-sent.php).

Re: How do you kill cookies once and for all??

Posted: Thu Jan 09, 2014 9:05 am
by omarel
Sorry guys, I really appreciate it. I'm not sure what to do though. I'm still fairly intermediate.

The code above is most of my code.

I'm setting the cookie on a login page and displaying the cookie on an index page. Then Unsetting the cookie on a separate logout page. But when you go back to the index page it still shows the cookie value.

If anyone has a sample that would be great.

I'm killing the cookie with this code but it's not working

<?php
if(isset($_COOKIE['name'])) {
unset($_COOKIE['name']);
setcookie('name', '', time() - 3600);
}
?>

Re: How do you kill cookies once and for all??

Posted: Thu Jan 09, 2014 3:20 pm
by social_experiment
HeyThere wrote:I mean that what @social_experiment wanted to say is that you should check if your headers were not already sent
No.

What does your original cookie setting code look like, on the login page?

Have you tried to remove the unset($_COOKIE['name']) line? According to the PHP manual the correct way to unset a cookie is to use a time value that is in the past
setcookie()

Re: How do you kill cookies once and for all??

Posted: Thu Jan 09, 2014 3:35 pm
by omarel
My cookie set looks like this:

Code: Select all

  setcookie('name', $name, time()+60*60*24*365, '/', 'domain.com');
My cookie sign out looks like this:

Code: Select all

if(isset($_COOKIE['name'])) {
  unset($_COOKIE['name']);
  setcookie('name', '', time() - 3600);
}
The cookie does not go away.

Re: How do you kill cookies once and for all??

Posted: Thu Jan 09, 2014 3:58 pm
by requinix
The "matching the cookie parameters" thing I mentioned before is making sure you use the same path and domain in the call to set the cookie as you do to remove the cookie. So

Code: Select all

setcookie('name', '', time() - 3600, '/', 'domain.com');

Re: How do you kill cookies once and for all??

Posted: Thu Jan 09, 2014 4:12 pm
by omarel
LIFE SAVER! That worked.

so it's this to set

Code: Select all

setcookie('name', $name, time()+60*60*24*365, '/', 'domain.com');
and this to remove

Code: Select all

 setcookie('name', '', time() - 3600, '/', 'domain.com'); // empty value and old timestamp