简体   繁体   中英

Setting Cookie Expiration Date

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?

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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM