storing a cookie indefinitely

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
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

storing a cookie indefinitely

Post by davidklonski »

hello there

I was wondering whether it is possible to set a cookie indefinitely?

I looked at the setcookie() API and the expiration parameter is a Unix timestamp.

Is there a way to send a cookie to the browser which will stay there forever (unless the user deletes it) ?

thanks in advance
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

yeah, just set the cookie to expire in like 40 years or something like that.

Mark
phait
Forum Commoner
Posts: 46
Joined: Wed Apr 07, 2004 4:41 am
Location: watford / leicester, UK

Post by phait »

hi,
Yes it should be possible, although you would have to set an expiration date into the future and then periodically update that expiration date. For example, you might set the cookie to expire a year into the future from when it is set. The whenever a cookied user comes back you simply update the expiration time to the current date + 1 year.

I haven't got the code to hand but that's the way I would do it. There isn't a definitive attribute - that I know of - that makes it live forever without some nursing of the date going on.
User avatar
johnperkins21
Forum Contributor
Posts: 140
Joined: Mon Oct 27, 2003 4:57 pm

Post by johnperkins21 »

Why would you make a cookie indefinite? Put it at 5 years or so. How long can you reasonably expect a person to come back to your site using the same PC (without having been formatted and re-loaded)? If you set it to last for 10 years, the cookie would invariably never expire. Or take Bech's idea of setting it to 40 years, but 10 is probably more than enough.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

I dont think there are much computers around that wont be formatted once every couple of ..... ? ( not years lol )
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

i think 1 year is enough. ol4pro says right... :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Last time I read the cookie spec, it said something like: for ~infinite cookies, just set to one year beyond current time. Anything else wasn't guaranteed to exist...
Post Reply