如何设置特定页面的会话时间

时间:2011-01-04 21:32:06

标签: php session

当你转到这个页面; http://www.ctuchicago.com/registration/login.php

你会看到我制作的表格。我想创建一个会话,如果用户名和密码是真的,我想为它设置20分钟的到期时间。我该怎么办?

my check.php

$user = $_POST['user'];
$pass = $_POST['pass'];

$ruser = "a";
$rpass = "b";

if ($user == $ruser) {
 $logn = "True"; 
} else {
 $logn = "False";  
}

if ($pass == $rpass) {
 $logs = "True"; 
} else {
 $logs = "False";  
}


 if ($logn == "False" && $logs == "False") {
 echo '<script type="text/javascript">alert("The Username and The Password are both wrong ! Please check your information and try again.");history.go(-1);</script>';
 } elseif ($logn == "True" && $logs == "False") {
 echo '<script type="text/javascript">alert("The Username is True but something is wrong with the password you entered. Please check your information and try again.");history.go(-1);</script>';
 } elseif ($logn == "False" && $logs == "True") {
 echo '<script type="text/javascript">alert("The Password is True but something is wrong with the password you entered. Please check your information and try again.");history.go(-1);</script>';
 } else {
 $_SESSION['valid'] = "1";
 echo '<script type="text/javascript">window.location = "http://www.ctuchicago.com/registration"</script>';
 }

我的index.php(此页面我想要登录。

    if ($_SESSION['valid']) {
 echo '<script type="text/javascript">window.location = "http://www.ctuchicago.com/registration/login.php"</script>';
} else { Code }

谢谢

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

您是否考虑过使用在20分钟后过期的[客户端] Cookie?然后,无论何时(重新)加载页面,检查cookie,如果cookie无效/过期,则使会话无效。

答案 2 :(得分:0)

请参阅How do I expire a session after 30 minutes

修改

基本上你正在做的是创建自己的会话超时功能。点击页面后,您会找到在$_SESSION中设置的变量,然后调用它$_SESSION['started']。如果找不到该变量,则创建它并给出当前时间戳的值。你所做的就是每次加载一个新页面时,你将$_SESSION['started']变量与现在的时间进行比较,如果它超过20分钟,那么你就会过期记录用户或做你需要做的任何事情。