我有这个登录类:
while($stmt->fetch()) {
$_SESSION['some_session'] = $username;
$_SESSION['time_session'] = time();
header("Location: home.php");
exit();
}
和我的user_online脚本:
public function check_if_logged_in() {
if(time() - $_SESSION['time_session'] > 3600) {
session_start();
$users->logout($_SESSION['some_session']);
session_start();
session_destroy();
header("Location: index.php?e=expired");
exit();
}
}
还有我的退出课程:
public function logout($username) {
$j = 0;
$stmt = $this->mysqli->prepare("UPDATE blog_users SET is_online=? WHERE username=?");
$stmt->bind_param('ss', $j, $username);
$stmt->execute();
$stmt->close();
}
我的问题:当用户尝试导航时,我的系统检查用户是否已经过了3600在线,如果他/她有,则将其记录下来。我想要发生的事情:没有用户导航,如果用户已经过了3600的在线时间,它应该在数据库中自动将他们的is_online状态更新为0,是否可能?您可能想知道我为什么要完成is_online表,这是因为我想在线显示用户。
答案 0 :(得分:2)
如果您希望用户自动退出,如果他/她不执行任何操作,您必须使用javascript检查或使用ajax并在某个地方存储每个操作..
javascrip解决方案
这样的事情应该起作用
jQuery(document).ready(function() {
countDown();
$('body')
.change(function() {//reset the counter if change any value in
//the page http://api.jquery.com/change/
resetCounter();
})
.mousemove(function() {//reset the counter if the mouse is moved inside the body element
//http://api.jquery.com/mousemove/
resetCounter()
})
.click(function() {//reset the counter if the click inside the body element
//http://api.jquery.com/click/
resetCounter()
});
$(window).scroll(function() {//reset the counter if make scroll
//http://api.jquery.com/scroll/
resetCounter()
});
});
var seconds = 3600; //set the var seconds to the time you want start to check
function countDown() {
if (seconds <= 0) {//when the time over execute
window.location = "logout.php";
}
seconds--;//run every second to decrees a second
window.setTimeout("countDown()", 1000);
}
function resetCounter() {//reset counter
seconds = 3600;
}