3

This is the code that I'm using which forces cookies to expire after 90 days:

setcookie("WHMCSAffiliateID", $aff, time()+90*24*60*60);

If I want the cookie to never be set to expire, what would I need to change this line to?

Rudie
  • 52,220
  • 42
  • 131
  • 173
user2090227
  • 83
  • 1
  • 6
  • Possible duplicate http://stackoverflow.com/questions/3290424/set-a-cookie-to-never-expire – Elbek Mar 02 '13 at 00:09

2 Answers2

0

How about

$year = 365*86400;
setcookie("WHMCSAffiliateID", $aff, time()+100*$year);

?

100 years is a bit much obviously. 2 years should be fine. Or 1. Or 10 if you have to be absolutely sure and never change your code.

Rudie
  • 52,220
  • 42
  • 131
  • 173
0

It is not possible to set a cookie to never expire. However, if you renew cookies periodically then they can effectively live forever.

Personally, I like to set cookies for 30 days, and renew them every so often. One way of doing this might be:

setcookie("WHMCSAffiliateID",$add,time()+30*24*60*60);
setcookie("Renew-cookies","1",time()+10*24*60*60);

Then on every pageload:

if( isset($_COOKIE['WHMCSAffiliateID']) && !isset($_COOKIE['Renew-cookies'])) {
    setcookie("WHMCSAffiliateID",$_COOKIE['WHMCSAffiliateID'],time()+30*24*60*60);
    setcookie("Renew-cookies","1",time()+10*24*60*60);
}
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Do you really do this way or its just an example of how to do it? – Yang Mar 02 '13 at 00:15
  • Well, in all honesty I just renew the cookies on every request with `foreach($_COOKIE as $k=>$v) setcookie($k,$v);` but I'll be the first to admit it's not a great idea! – Niet the Dark Absol Mar 02 '13 at 00:20
  • Yeah, that's just horrible. In any application modern application cookies should be abstracted and encapsulated within a `Cookie` class. – Yang Mar 02 '13 at 00:27
  • Pfffthhht (or however you spell that sound). Been working fine for me since I started programming. – Niet the Dark Absol Mar 02 '13 at 00:30