在 php 中处于非活动状态 15 分钟后自动注销
2022-08-30 14:43:09
如果用户没有在网站上进行任何类型的活动,我想销毁会话。当时5个用户自动重定向到索引页上。这怎么可能?在php中可以进行会话处理,为此我必须维护或更新用户登录时间。
如果用户没有在网站上进行任何类型的活动,我想销毁会话。当时5个用户自动重定向到索引页上。这怎么可能?在php中可以进行会话处理,为此我必须维护或更新用户登录时间。
这相对容易通过这里的这个小片段实现:
if(time() - $_SESSION['timestamp'] > 900) { //subtract new timestamp from the old one
echo"<script>alert('15 Minutes over!');</script>";
unset($_SESSION['username'], $_SESSION['password'], $_SESSION['timestamp']);
$_SESSION['logged_in'] = false;
header("Location: " . index.php); //redirect to index.php
exit;
} else {
$_SESSION['timestamp'] = time(); //set new timestamp
}
我从 Sitepoint.com 那里得到了这个解决方案 在你的html中使用一个简单的元标记
<meta http-equiv="refresh" content="900;url=logout.php" />
900 是您希望会话在处于非活动状态时终止的时间(以秒为单位)。
希望它对您有用
编辑:此方法不实现任何其他逻辑,因此仅当您想要“强制”注销时才能工作,如注释中所述