Deleting a Cookie - help required

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
smcpoland
Forum Newbie
Posts: 4
Joined: Wed Nov 05, 2003 8:31 am
Contact:

Deleting a Cookie - help required

Post by smcpoland »

Hi all;

first I have the following code at the beginning of the page

Code: Select all

<?php 
session_start();

if (!isset($_COOKIE&#1111;'Channel-Travel'])) 
&#123;
setcookie ("CIA", strip_tags(session_id()), time()+3600*24*365, "/", "www.CIA.com",0);
&#125;
?>
Which works wonderfully and if I check the Temp Files Folder (Windows XP Pro) the cookie is there in all its glory with the SID etc. The expiry is as expected - one year hence.

however I am trying to reset the cookie so i can reuse it

Code: Select all

session_unset();
session_destroy();
setcookie ("CIA", "", time()-3600*24*365, "/", "www.CIA.com",0);
it does not delete the cookie or reset the date.

First question is when would I expect to see the cookie deleted - immediately because this is what I expect.

In the if (!isset($_COOKIE['Channel-Travel'])) above does it consider a 'deleted cookie' set or not?

This is annoying me greatly - Thanks in advnace for your help

regards
Sean



?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Wierd.
Try:

Code: Select all

setcookie ("CIA", strip_tags(session_id()), time()-3600, "/", "www.CIA.com",0)
...meaning; don't discard the cookied value when erasing it. Just a sot in the dark.

isset() should see if it's set, but imho a better usage would be !empty() as that surely gives you true/false if it actually contains a value, not only if it's set.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

The manual says:
If only the name argument is present, the cookie by that name will be deleted from the remote client.
Try this. Hope it'll help you.
Although in their examples they use the date-in-the-past trick.
smcpoland
Forum Newbie
Posts: 4
Joined: Wed Nov 05, 2003 8:31 am
Contact:

Deleting Cookies - Got it

Post by smcpoland »

First Thanks for your help it allowed me to think and you raised a couple of points:

Second I found two things that I noticed others might want to be aware of:

My security level thru Norton Internet Security restricts REFERRER - bang - I had to set up a personal rule to my site allowing referrer to be referenced.

During my testing I was not completing the script as such - if the script fails for example:

echo "tp1";
setcookie.....
header (location)

(the header fails due to the echo) the setcookie will not complete - the script must finish for the setcookie to work.

As a sequential interpretative language I would have expected the setcookie to work no problem - it looks like php does not get rid of the cookie until the script finishes correctly............

Thanks again...
regards to all
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Deleting Cookies - Got it

Post by JAM »

smcpoland wrote:(the header fails due to the echo) the setcookie will not complete - the script must finish for the setcookie to work.
A fairly discussed topic.
viewtopic.php?t=1157

The Norton Internet Security issue was interesting.

Sidenote:
As Weirdan states, only using the name argument is another solution, but have in mind that it might interfere with multible cookies.
Post Reply