【问题标题】:PHP session expires after some time limit [duplicate]PHP会话在一段时间后过期[重复]
【发布时间】:2013-07-15 16:10:09
【问题描述】:

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

?>

【问题讨论】:

    标签: php session


    【解决方案1】:

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

    在body标签中

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

    【讨论】:

    • 这是一个不错的工作脚本,我刚刚使用它,但会尝试如何使用会话来完成。
    • 在您的会话开始的索引页面中使用上述代码,您可以在注销页面中销毁会话..它重新加载到索引页面
    【解决方案2】:

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

    在用户登录时添加。

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

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

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

    【讨论】:

    • $_SESSION['start_time'] = strtotime("now");这将通过 echo 显示已经开始的会话,但第二部分对我不起作用。
    猜你喜欢
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 2015-09-16
    • 2018-05-25
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 2012-12-18
    相关资源
    最近更新 更多