NYCPHP Meetup

NYPHP.org

[nycphp-talk] cookies and experiation

Margaret Waldman mmwaldman at nyc.rr.com
Sun Jul 10 12:09:46 EDT 2011


I’ve thought of all of this ...


- is using the same computer for five years
- has never reformatted the hard drive or re-installed a fresh OS
- has never switched browsers
- has never cleared their cookies

Thanks for PHP_INT_MAX.  
From: Jim Yi 
Sent: Sunday, July 10, 2011 10:44 AM
To: NYPHP Talk 
Subject: Re: [nycphp-talk] cookies and experiation

PHP has some useful predefined constants, particularly PHP_INT_MAX in this case (http://php.net/manual/en/reserved.constants.php).  What you should really be doing while setting the cookie is ignoring time() completely, and just set the expiration time to the maximum size of an integer.

  setcookie(“name”, “val”, PHP_INT_MAX);

This way, the cookie expires in 2038 for everyone, which is the furthest expiration you can set anyway.

On a more theoretical note, there really isn't a need to set a cookie's expiration date beyond, let's say 5 years.  For that cookie to even still be alive, you have to hope that the user:
- is using the same computer for five years
- has never reformatted the hard drive or re-installed a fresh OS
- has never switched browsers
- has never cleared their cookies

I'd say a combination of all of the above is extremely rare, and I can't really think of a good use-case where a cookie would still be useful 5 years from now, given that the internet is constantly evolving.

---
Jim Yi



On Sun, Jul 10, 2011 at 9:56 AM, Margaret Waldman <mmwaldman at nyc.rr.com> wrote:

  I wanted to create a cookie that basically never expires.

  time() + x

  We can live to say 100 or so, so say 125 years expiration would be good.

  time() + 3944700000

  But that number is too big.

  Intval says on a 32 bit machine max is 2147483647.

  So 2147483647 – time() = 4294967294, which is bigger than 2147483647

  2147483647 = 68 years but because I’m adding time it grows bigger than the bigger integer

  But I really can only do this 2147483647  - time() = 837178076 26 years

  But php wasn’t crapping out on 290000000 + time(), so what really is the biggest integer

  Time today =            1310305571
  Time tomorrow =     1310391971
  Next year will be =   1341863171 (42 years)
  In 20 years will be = 1372420841 (52 years)

  So, 2147483647 – time in 20 year (1372420841) = 775062806 approx 23 years

  The numbers don’t look quite right but at time increases the life time you can extend in ints decreases.

  So, instead of just adding a value to time, I should

  $newLife = 2147483647  - time();

  setcookie(“name”, “val”, time() + newLife);

  I’m sure by the time, time() grows to be so big that this formula is an issue, integers will be big enough to handle it?

  Anyway, does anyone know the real max size of an integer? Cause it ain’t 2147483647 like the docs say for a 32 bit machine.

  Margaret Michele Waldman

  _______________________________________________
  New York PHP Users Group Community Talk Mailing List
  http://lists.nyphp.org/mailman/listinfo/talk

  http://www.nyphp.org/Show-Participation





--------------------------------------------------------------------------------
_______________________________________________
New York PHP Users Group Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

http://www.nyphp.org/Show-Participation
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20110710/4aba4b78/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: wlEmoticon-smile[1].png
Type: image/png
Size: 1041 bytes
Desc: not available
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20110710/4aba4b78/attachment.png>


More information about the talk mailing list