Where and when do I delete my cookie on the page?

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
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Where and when do I delete my cookie on the page?

Post by ljCharlie »

I have my cookie set as setcookie("UserName", $userName, time()+3600); on the very top of the page. Now, where and when do I delete my cookie on the page?

Any help is grateful!

ljCharlie
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

In my experience, you can't delete the cookie, just change it's value. A second call to setCookie does it fine.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

You can delete a cookie by setting its time in the past

Code: Select all

setcookie("UserName", $userName, time()-3600);
or by giving a blank value

Code: Select all

setcookie("UserName", '', time()+3600);
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Yeah Grims method is best, however you can use javascript from what I know but it is not as reliable.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Yes, best method is a combination of the two:

Code: Select all

setcookie("UserName", '', time()-3600);
Of course, bear in mind that your cookie expires (deletes itself) one hour after it was set anyway.

A cookie set without a time deletes itself as soon as the user browses away from your site or closes their browser.

Code: Select all

setcookie("UserName", '$username');
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Ah! So if I already have my cookie set to expire after one hour then I don't have to menually set the time in reverse to delete the cookie, correct?

ljCharlie
User avatar
code_monkey
Forum Newbie
Posts: 16
Joined: Tue Jul 08, 2003 6:13 am
Location: UK
Contact:

Post by code_monkey »

why not just use session variables?
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

What is session variables and how do I implement it?

ljCharlie
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Grim... wrote: A cookie set without a time deletes itself as soon as the user browses away from your site or closes their browser.
Session cookie (cookie without a time set) gets deleted only when you close your browser. phpBB, for example, does use session cookie. Grim, you could navigate away from this forums, then return back and be still logged in.
User avatar
maqmus
Forum Commoner
Posts: 30
Joined: Mon Mar 08, 2004 1:10 pm

Post by maqmus »

Have a look here
Post Reply