最近某个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; // 刷新时间戳
}
}
根据文章《
http://www.jb51.net/article/52961.htm》,我们可以结合第一种和第二种方法来最终决定会话超时时间。
Statement: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