PHP会话在一段时间限制后到期

时间:2013-07-17 07:51:24

标签: php session

我想在会话到期后10分钟内从index.php自动注销。请帮帮忙?

我已经有了这个:

//this is login.php
//register the session for user and password
session_register("userName");
session_register("password");
if($userType=="Web_User"){
header("location:index.php?");
} 

//index.php
//check session start or not
    <?php
if (!isset($_SESSION['start_time']))
{
    $str_time = time();
    $_SESSION['start_time'] = $str_time;
}
echo $_SESSION['start_time'];

//here I want to expired if user inactive for 10 minutes and redirect to the login.php

?>

2 个答案:

答案 0 :(得分:0)

您可以像这样设置php session timeout或硬编码。

在用户登录时添加此项。

$_SESSION['start_time'] = strtotime("now");

将此添加到您想要检查它们是否已经过10分钟的位置。

if($_SESSION['start_time'] <= strtotime("-10 minutes"))
{
    //Log them out.
}

答案 1 :(得分:0)

我在stackoverflow中找到了这个

        <script>
var timer = 0;
function set_interval() {
timer = setInterval("auto_logout()", 600000);
// set to 10 minutes
}

function reset_interval() {
//resets the timer. The timer is reset on each of the below events:
// 1. mousemove   2. mouseclick   3. key press 4. scroliing
//first step: clear the existing timer

if (timer != 0) {
clearInterval(timer);
timer = 0;
// second step: implement the timer again
timer = setInterval("auto_logout()", 600000);
// completed the reset of the timer
} 
}

function auto_logout() {
 // this function will redirect the user to the logout script
 window.location = "logout.php";
}
</script>

和身体标签

onLoad="set_interval();" onmousemove="reset_interval();" onclick="reset_interval();" onkeypress="reset_interval();" onscroll="reset_interval();"