我有一个用户可以有很多朋友的社区网站。在显示他所有的朋友时,我想包括他的朋友是在线还是离线。
我的方法是,当用户登录时,在状态列“在线”上创建一个会话并更新用户表。如果他单击注销按钮,那么我会将状态设置为“离线”。如果他没有点击注销按钮就关闭了浏览器怎么办?这是我想做的事情:
session_start();
if (!isset($_SESSION['LAST_ACTIVITY'])) {
// initiate value
$_SESSION['LAST_ACTIVITY'] = time();
}
if (time() - $_SESSION['LAST_ACTIVITY'] > 3600) {
// last activity is more than 10 minutes ago
session_destroy();
//direct to a php, say this user is idle and thus status = offline
header("location: update_status.php?user=".$_SESSION['username']."&status=offline");
// den redirect them to login page
} else {
// update last activity timestamp
$_SESSION['LAST_ACTIVITY'] = time();
}
这是合适的方式吗?
编辑:
看看一些简单的示例代码会很有帮助,如何检查用户何时在线并在用户访问页面时更新?
我需要包含php?user=$_SESSION['userid']
在每个链接中吗?