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

帅比萌擦擦* 提交于 2019-12-01 21:36:23

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.

You should not set cookies to unlimited time. If you need to store something for a longer period, use localStorage. They don't expire. They're not really cookies but it's also a way of setting information in the browser. You just have to use JavaScript with this.

As for PHP, what you can do is you can rely on databases that will store your information permanently. You can set things in local storage that you can use to load session information. But my suggestion is just use localStorage.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!