0

我想在 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

?>
4

2 回答 2

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();"
于 2013-07-17T08:37:53.890 回答
0

您可以像这样设置php 会话超时或硬编码。

将此添加到用户登录时。

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

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

if($_SESSION['start_time'] <= strtotime("-10 minutes"))
{
    //Log them out.
}
于 2013-07-17T07:57:29.783 回答