Set a cookie to never expire

PhpCookies

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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).

http://www.faqs.org/rfcs/rfc2616.html

Solution 3 - Php

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

Solution 4 - Php

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.

Solution 5 - Php

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.

Solution 6 - Php

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 60602436510, which should outlive most of the machines your cookie will live on.

Solution 7 - Php

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.

Solution 8 - Php

Never and forever are two words that I avoid using due to the unpredictability of life.

The latest time since 1 January 1970 that can be stored using a signed 32-bit integer is 03:14:07 on Tuesday, 19 January 2038 (231-1 = 2,147,483,647 seconds after 1 January 1970). This limitation is known as the Year 2038 problem

setCookie("name", "value", strtotime("2038-01-19 03:14:07"));

Solution 9 - Php

You can set a far date from the date, the cookie is created like this:

var Cookie_expiry = new Date();
Cookie_expiry.setDate(Cookie_expiry.getDate()+10e5);   
// expiry date of cookie is set to be practically infinite (~ 4000 years)
setCookie('token', 'token_value', {expires: Cookie_expiry});

Solution 10 - Php

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.

Solution 11 - Php

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 :)

Solution 12 - Php

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" ...

Solution 13 - Php

You can't but what if you set expire time to now + 100 years ?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbrainimusView Question on Stackoverflow
Solution 1 - PhpJoeri HendrickxView Answer on Stackoverflow
Solution 2 - PhpPiTheNumberView Answer on Stackoverflow
Solution 3 - PhpDavidView Answer on Stackoverflow
Solution 4 - PhpJohnView Answer on Stackoverflow
Solution 5 - PhpJesusView Answer on Stackoverflow
Solution 6 - Phph3r2onView Answer on Stackoverflow
Solution 7 - PhpBjörnView Answer on Stackoverflow
Solution 8 - PhpPedro LobitoView Answer on Stackoverflow
Solution 9 - PhpAYUSH JAINView Answer on Stackoverflow
Solution 10 - PhpJoel KennedyView Answer on Stackoverflow
Solution 11 - PhpSarfrazView Answer on Stackoverflow
Solution 12 - PhppaulgavrikovView Answer on Stackoverflow
Solution 13 - PhpBoris DelormasView Answer on Stackoverflow