[英]Why won't my session time out log out not work?
It should logout after 20 mins of inactivity but it logs out after 3-4 seconds, I don't know where I'm going wrong. 它应该在闲置20分钟后注销,但在3-4秒后注销,我不知道我要去哪里。 I know other questions are out there but I've looked and can't find fix.
我知道还有其他问题,但是我已经查看了并且找不到解决方法。
<?php
session_start();
if ((isset($_SESSION['Username']) && $_SESSION['Username'] != "") || (isset($_SESSION['uID']) && $_SESSION['uID'] != "") || (isset($_SESSION['Password']) && $_SESSION['Password'] != "")) {
$selectUsers = $PDO->query("SELECT * FROM `users` WHERE uID='{$_SESSION['uID']}'");
$getRow = $selectUsers->fetch(PDO::FETCH_ASSOC);
if ($getRow['lasthit'] == time()+20*60) {
session_destroy();
session_unset();
$PDO->query("UPDATE `users` SET online=0 WHERE uID='{$_SESSION['uID']}'");
header("Refresh:3; URL=login.php");
} else {
$updateLastHit = $PDO->query("UPDATE `users` SET lasthit=NOW() WHERE uID='{$_SESSION['uID']}'");
}
} else {
header("Refresh:3; URL=login.php");
echo '<p class="error">You need to login, you are being redirected to login page.</p>';
endPage();
}
?>
Assuming that $getRow['lasthit']
contains the correct value, I think that you're using the wrong formula here: 假设
$getRow['lasthit']
包含正确的值,我认为您在这里使用了错误的公式:
if ($getRow['lasthit'] < time()+20*60) {
With the current code, it will always return true since time() will return a value higher than $getRow['lasthit']
. 使用当前代码,由于time()返回的值将大于
$getRow['lasthit']
,因此它将始终返回true。 This formula should work: 该公式应该起作用:
if ((time() - $getRow['lasthit']) > 20*60) {
if ($getRow['lasthit'] == time()+20*60) {
should be: 应该:
if ($getRow['lasthit'] < time()-20*60) {
You're looking for an exact match, and you're also adding instead of subtracting. 您正在寻找一个完全匹配的对象,同时您也在添加而不是减去。
Change your SELECT query to: 将您的SELECT查询更改为:
$selectUsers = $PDO->query("SELECT UNIX_TIMESTAMP(lasthit) lasthit FROM `users` WHERE uID='{$_SESSION['uID']}'");
so that it will return an integer, not a timestamp, which you can compare with PHP's now()
. 这样它将返回一个整数,而不是一个时间戳,您可以将其与PHP的
now()
进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.