是否可以设置一个cookie到会话变量的无限时间? [英] is it possible to set unlimited time for a cookie to a session variable?
问题描述
如何为会话设置无限时间的Cookie?我已经尝试以下,但我仍然得到未定义的索引通知在我的会话一天后:
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:
setcookie('idcourse', 'CourseID', 9999999999);
setcookie('namecourse', 'CourseName', 9999999999);
setcookie('id', 'ID', 9999999999);
if (isset($_POST['idcourse'])) {
$_SESSION['idcourse'] = $_POST['idcourse'];
}
if (isset($_POST['namecourse'])) {
$_SESSION['namecourse'] = $_POST['namecourse'];
}
if (isset($_POST['id'])) {
$_SESSION['id'] = $_POST['id'];
}
推荐答案
添加到期日期,或者Cookie将像会话一样,并在您离开网站时过期。
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;
您设置的过期日期为9999999999(您需要在将来指定UNIX TIMESTAMP),因此我使用以下命令:
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 );
将使cookie在2个月内过期,您可以相应地增加此值。
will make the cookie expire in 2 months, you can increment this accordingly.
这篇关于是否可以设置一个cookie到会话变量的无限时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!