Looking at the php documentation on setting a cookie I see that I can set an expiration date for the cookie. You can set the cookie to expire at the end of the browser session or at some time in the future but I do not see a way to set the cookie to never expire. Is this even possible and how is this accomplished?
All cookies expire as per the cookie specification, so this is not a PHP limitation.
Use a far future date. For example, set a cookie that expires in ten years:
setcookie(
"CookieName",
"CookieValue",
time() + (10 * 365 * 24 * 60 * 60)
);
Note that if you set a date past 2038 in 32-bit PHP, the number will wrap around and you'll get a cookie that expires instantly.
I believe that there isn't a way to make a cookie last forever, but you just need to set it to expire far into the future, such as the year 2100.
You can't but what if you set expire time to now + 100 years ?
You shouldn't do that and that's not possible anyway, If you want you can set a greater value such as 10 years ahead.
By the way, I have never seen a cookie with such requirement :)
While that isn't exactly possible you could do something similar to what Google does and set your cookie to expire Jan 17, 2038 or something equally far off.
In all practicality you might be better off setting your cookie for 10 years or 60*60*24*365*10, which should outlive most of the machines your cookie will live on.
I'm not sure but aren't cookies deleted at browser close? I somehow did a never expiring cookie and chrome recognized expired date as "at browser close" ...
My privilege prevents me making my comment on the first post so it will have to go here.
Consideration should be taken into account of 2038 unix bug when setting 20 years in advance from the current date which is suggest as the correct answer above.
Your cookie on January 19, 2018 + (20 years) could well hit 2038 problem depending on the browser and or versions you end up running on.
Set a far future absolute time:
setcookie("CookieName", "CookieValue", 2147483647);
It is better to use an absolute time than calculating it relative to the present as recommended in the accepted answer.
The maximum value compatible with 32 bits systems is:
2147483647 = 2^31 = ~year 2038
Can't you just say a never ending loop, cookie expires as current date + 1 so it never hits the date it's supposed to expire on because it's always tomorrow? A bit overkill but just saying.
If you want to persist data on the client machine permanently -or at least until browser cache is emptied completely, use Javascript local storage:
https://developer.mozilla.org/en-US/docs/DOM/Storage#localStorage
Do not use session storage, as it will be cleared just like a cookie with a maximum age of Zero.
Maximum value: 2147483647
setcookie("CookieName", "CookieValue", 2147483647);
To avoid integer overflow the timestamp should be set to:
2^31 - 1 = 2147483647 = 2038-01-19 04:14:07
Setting a higher value might cause problems with older browsers.
Also see the RFC about cookies:
Max-Age=value OPTIONAL. The value of the Max-Age attribute is delta-seconds, the lifetime of the cookie in seconds, a decimal non-negative integer. To handle cached cookies correctly, a client SHOULD calculate the age of the cookie according to the age calculation rules in the HTTP/1.1 specification [RFC2616]. When the age is greater than delta-seconds seconds, the client SHOULD discard the cookie. A value of zero means the cookie SHOULD be discarded immediately.
and RFC 2616, 14.6 Age:
If a cache receives a value larger than the largest positive integer it can represent, or if any of its age calculations overflows, it MUST transmit an Age header with a value of 2147483648 (2^31).