如果用户处于非活动状态 15 分钟,如何使 php 会话过期 [英] how to expire php session if user is inactive for 15 mins

查看:37
本文介绍了如果用户处于非活动状态 15 分钟,如何使 php 会话过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 PHP 创建了一个项目,我正在其中管理会话.

i have created one project in PHP, into which i am managing sessions.

我正在通过编写以下代码行在我的 config.php 文件中创建会话.

I am creating session in my config.php file by writing following line of code.

session_start();

为了销毁这个会话,在 logout.php 文件中,我写了以下行.

and to destroy this session, in logout.php file i have write following line.

session_destroy();

并且我没有在任何其他项目文件中提到任何会话代码,但问题是会话处于活动状态,直到我调用 logout.php,

and i have not mention any code for session in any other project file, but the problem is session is active untill i call logout.php,

我想要的是,如果用户处于非活动状态 15 分钟,会话应该过期.

what i want is session should expire if user is inactive for 15 minutes.

任何人都可以帮助我吗,我是 PHP 新手,请提供一些示例代码或链接来实现这一点..

can anyone help me for this, i am new to PHP, please give some example code or link to achieve this..

推荐答案

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

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.

这篇关于如果用户处于非活动状态 15 分钟,如何使 php 会话过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆