Page 1 of 1

How to delete a cookie when a user leaves a page?

Posted: Tue Jun 08, 2004 8:29 am
by ljCharlie
I know how to delete a cookie but my question is, how do I delete a cookie at the time the user leaves a certain page? As long as the user is still on a profile page, let's say, keep the cookie but as soon as the user leaves the profile page to a different page then delete the cookie so I can reset the cookie to a different value. Can this be done? If so, will you mind show me how?

Thank you,

ljCharlie

Posted: Tue Jun 08, 2004 9:05 am
by patrikG
set the expiry-date to some date in the past or update the value of your cookie.

Posted: Tue Jun 08, 2004 9:08 am
by Grim...
No, that wouldn't work because headers have been sent.

Posted: Tue Jun 08, 2004 9:10 am
by ljCharlie
Many thanks for your response. Okay, let's say I set the cookie at the begining of the page called profile.php. Now when the user browse to the page called login.php, where do I set the expire date, on the begining of the login.php page or at the very end of the profile.php page?

By the way, how do I update the value in the cookie?

ljCharlie

Posted: Tue Jun 08, 2004 9:13 am
by Grim...
Update the value by overwriting it (set the cookie again).

You'd have to set the expire date at the beginning (before any HTML) of the next page the user hits.

Posted: Tue Jun 08, 2004 9:16 am
by patrikG
At the top, otherwise you get the header-error.

Cookie tutorial#1: http://www.juicystudio.com/tutorial/php/cookies.asp
Cookie tutorial#2: http://www.spoono.com/php/tutorials/tut ... rl=cookies
Cookie tutorial#3: http://www.zend.com/zend/tut/feedback.php

and, lastly:

viewtopic.php?t=11417

Posted: Tue Jun 08, 2004 11:59 am
by evilMind
use javascript to delete the cookie.
eg:

Code: Select all

<body onunload="document.cookie = 'yourCookieName=yourCookieValue; expires=Sat, 10-Jun-2000 00:00:00 GMT;';">
... Your page ...
</body>

Posted: Tue Jun 08, 2004 7:22 pm
by ljCharlie
Many thanks for all your help. I'll give that a try.

ljCharlie