How do i make a cookie for an indefinate period of time, I never want it to expire?
| SitePoint Sponsor |

How do i make a cookie for an indefinate period of time, I never want it to expire?
Cheers
Coup



I don't think you can specify a value that means "never" expire, but you can set the value for a very long time, i.e. 50 years.
PHP Code:setcookie("test", "testme", time()+60*60*24*365*50);

i though the date system used in a cookie was similar to a unix time stamp, and doesn't that only go up to something 2027, admittedly thats long enough but i was just wondering.
or is the cookie date differant?
Cheers
Coup



Yes, you are correct January 19, 2038 is the date at which timestamps will overflow. I wasn't thinking, so you want something smaller than fifty years![]()





Generally cookies that are intended to last "forever" are set to 1 year, and then refreshed (i.e. send the cookie again with a new expiration date 1 year hence) on every page load. This avoids the overflow problem (which actually is not inherent to the cookie itself but to systems that store the date in a Unix timestamp format - cookies use the format "Tue, 12-Jun-2008 17:38:45 GMT") while still giving you your "forever cookie".
Bookmarks