How do you kill cookies once and for all??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
omarel
Forum Newbie
Posts: 9
Joined: Wed Jan 08, 2014 1:43 pm

How do you kill cookies once and for all??

Post 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';}
?>
Last edited by omarel on Thu Jan 09, 2014 5:36 am, edited 2 times in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post by social_experiment »

yes it seems you have to close the browser instance to kill the cookies' persistance
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
omarel
Forum Newbie
Posts: 9
Joined: Wed Jan 08, 2014 1:43 pm

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

Post by omarel »

Closing the browser doesnt work either
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

Are you sure you're clearing (trying to) before you've outputted anything? Have you matched the cookie parameters - domain and path?
omarel
Forum Newbie
Posts: 9
Joined: Wed Jan 08, 2014 1:43 pm

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

Post 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?
HeyThere
Forum Newbie
Posts: 2
Joined: Thu Jan 09, 2014 7:19 am

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

Post 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).
omarel
Forum Newbie
Posts: 9
Joined: Wed Jan 08, 2014 1:43 pm

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

Post 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);
}
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post 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()
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
omarel
Forum Newbie
Posts: 9
Joined: Wed Jan 08, 2014 1:43 pm

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

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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');
omarel
Forum Newbie
Posts: 9
Joined: Wed Jan 08, 2014 1:43 pm

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

Post 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
Post Reply