Checking cookie using if isset and then deleting it

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
BleedingEdge
Forum Newbie
Posts: 2
Joined: Mon May 25, 2009 3:51 am

Checking cookie using if isset and then deleting it

Post by BleedingEdge »

Hi guys,

I'm new here so 'hello' :D

I have a question about checking a client cookie using if isset() and then destroying the cookie.

Given a cookie called "cookiename", I check it like this:

Code: Select all

if (isset($_COOKIE['cookiename']))
Which is working fine. But, then a few lines later when I have loaded the cookie value into a variable, I delete the cookie using:

Code: Select all

setcookie('cookiename');
This overwrites the cookie with blank data, but *the cookie still exists*.

Will if (isset($_COOKIE['cookiename'])) return true because the cookie still exists? Even though the value is blank?

It appears to be working fine (ie, the cookie is not being read once overwritten), but I just want to be sure....! Do I need to add another conditional after if isset (for example IF the cookie is set OR the cookie value is null) or is what I am doing ok?

Many thanks,

Martin
Last edited by Benjamin on Tue May 26, 2009 10:35 am, edited 1 time in total.
Reason: Changed code type from text to php.
nakeddeveloper
Forum Newbie
Posts: 4
Joined: Wed Feb 25, 2009 10:58 am

Re: Checking cookie using if isset and then deleting it

Post by nakeddeveloper »

I imagine it will still see it as set, would destroy it by setting a expiry time in the past on it like so...
setcookie("cookiename","",time()-3600);
BleedingEdge
Forum Newbie
Posts: 2
Joined: Mon May 25, 2009 3:51 am

Re: Checking cookie using if isset and then deleting it

Post by BleedingEdge »

Ah! Thank you.

I tried that before, and it appeared not to work (in FireFox I could still see the cookie in the "show cookies" window), but, on refreshing the window (closing and opening), the cookie had gone!

Thanks,

Martin
Post Reply