Home > php教程 > php手册 > body text

一个严格的PHP Session会话超时时间设置方法

WBOY
Release: 2016-06-13 09:34:04
Original
821 people have browsed it

最近某个PHP项目用到了限制登录时间的功能,比如用户登录系统60分钟后如果没有操作就自动退出,我搜索了网络收集了有以下方法可供参考。

第一种方法即设置php.ini配置文件,设置session.gc_maxlifetime和session.cookie_lifetime节点属性值,当然也可以使用ini_set函数改变当前上下文环境的属性值:

复制代码 代码如下:


ini_set('session.gc_maxlifetime', "3600"); // 秒
ini_set("session.cookie_lifetime","3600"); // 秒



第二种方法即设置Session时间戳,比如下面的办法。

在登录成功时设置时间戳为当前时间推后1小时,$_SESSION['expiretime'] = time() + 3600;。在检查用户登录情况使用如下代码:

复制代码 代码如下:


if(isset($_SESSION['expiretime'])) {
    if($_SESSION['expiretime']         unset($_SESSION['expiretime']);
        header('Location: logout.php?TIMEOUT'); // 登出
        exit(0);
    } else {
        $_SESSION['expiretime'] = time() + 3600; // 刷新时间戳
    }
}


根据laruence大神的文章《如何设置一个严格30分钟过期的Session》,我们可以结合第一种和第二种方法来最终决定会话超时时间。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!