is it possible to set unlimited time for a cookie to a session variable?

后端 未结 2 876
一整个雨季
一整个雨季 2021-01-20 03:13

How do I set unlimited time to a cookie for a session? I have tried the following below but I still get undefined index notices on my sessions after a day:

s         


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-01-20 03:55

    You must add an expiry date, or the cookie will act like a session and expire when you leave the website,

    what you're doing is nearly right but you need to change it slightly;

    You are setting expire 9999999999 (you need to specify a UNIX TIMESTAMP in the future), so i use the following:

    $inTwoMonths = 60 * 60 * 24 * 60 + time();
    setcookie('idcourse', 'CourseID', $inTwoMonths );
    setcookie('namecourse', 'CourseName', $inTwoMonths );
    setcookie('id', 'ID', $inTwoMonths );
    

    will make the cookie expire in 2 months, you can increment this accordingly.

提交回复
热议问题