[英]Users Online PHP
我有这个登录课程:
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表,这是因为我想在线显示用户。
如果您希望用户不执行任何操作而自动注销,则必须使用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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.