且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果用户处于非活动状态 15 分钟,如何使 php 会话过期

更新时间:2023-11-28 07:56:46

在你的头文件中调用下面的函数,这样每当用户在那个时候进行任何活动时页面都会刷新并检查会话是否超时.

Call below function in your header file, so that whenever user does any activity at that time page gets refreshed and check whether session time outs or not.

function auto_logout($field)
{
    $t = time();
    $t0 = $_SESSION[$field];
    $diff = $t - $t0;
    if ($diff > 1500 || !isset($t0))
    {          
        return true;
    }
    else
    {
        $_SESSION[$field] = time();
    }
}

在标题中使用类似的内容

Use something like this in header

    if(auto_logout("user_time"))
    {
        session_unset();
        session_destroy();
        location("login.php");          
        exit;
    }       

User_time 是会话名称.我希望这个答案能帮到你.这段代码的实际作用是:检查 diff 是否大于 1500 秒.如果不是,则设置新的会话时间."您可以根据需要更改时间差异(1500).

User_time is the session name. I hope this answer will help you. What actually this code does is : "Checks whether diff is greater than 1500 seconds or not. If not then set new session time." You can change time diff(1500) according to your requirement.