这个问题在这里已有答案:
我正在使用这段代码进行手动注销,但我希望在网站上闲置5分钟后自动注销。怎么做?谢谢
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_destroy();
if(!session_is_registered('username')) {
header("location: logout_msg.html");
} else {
print "<h2>Could not log you out, sorry the system encountered an error.</h2>";
}
exit();
?>
您可以尝试使用$_SESSION['CREATED']
来存储时间戳,然后检查您的值是否在$_SESSION['CREATED'] + 300
之前创建。
在每个网页浏览中保存会话中的当前时间戳。如果当前时间戳与会话值的时间间隔超过五分钟,请检查该值并使其会话无效。
这一切都取决于你如何定义不活动。我这样做
if ($_SESSION['LAST_ACTIVITY'] < time() - (60 * 5)) {
// logout
}
$_SESSION['LAST_ACTIVITY'] = time();