我有一个登录类:
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表,因为我想在网上向用户展示。
发布于 2014-02-09 06:38:29
如果您希望用户自动退出,如果他/她不执行任何操作,则必须使用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;
}https://stackoverflow.com/questions/21656150
复制相似问题