如果我有一个带有unix time()标记的列“lastaccess
”,我该如何过滤掉在线用户。然后我通常有这个功能:
function showStatus($userid) {
$timeoutIdle = 80;
$timeoutOffline = 150;
$row = mysql_query("select last_access from users where id='$userid'");
$read = mysql_fetch_array($row);
$last_access = $read["last_access"];
$thetime = time();
if ($thetime - $last_access > $timeoutOffline) {
echo "";
}
else if ($thetime - $last_access > $timeoutIdle) {
echo "<span class='statusIdle'>Idle</span>";
} else {
echo "<span class='statusOnline'>Online</span>";
}
}
如何进行查询,如果您在线,它会显示在哪里?
答案 0 :(得分:1)
$sql = "SELECT
CASE
WHEN last_access < UNIX_TIMESTAMP()-150 THEN 'offline'
WHEN last_access < UNIX_TIMESTAMP()-80 THEN 'idle'
ELSE 'online'
END AS online_status
FROM users
WHERE id = $userid";
答案 1 :(得分:0)
MySQL有一些与Date/Times相关的功能。查看NOW()
和UNIX_TIMESTAMP()
。您可以将此添加到您的查询where子句SELECT
仅限在线用户。
SELECT * FROM users WHERE NOW() - last_access < 80;
或使用CASE
语句确定给定用户的状态字符串
SELECT CASE
WHEN (NOW() - last_access > 150) THEN 'offline'
WHEN (NOW() - last_access > 80) THEN 'idle'
ELSE 'online' END AS status
FROM users WHERE id = $user_id;
答案 2 :(得分:0)
我想说一个更简单的方法是, 当用户登录时更新该用户的状态字段:
$update = mysql_query("UPDATE table SET status='online' WHERE id='$userid'")
or die(mysql_error());
然后在您要查看他们是否在线的页面上添加以下内容:
$status = mysql_query("SELECT online FROM table
WHERE id='$searched_user'") or die(mysql_error());
$row = mysql_fetch_array( $status );
if($row['status'] == "online"){
echo "Online";
}
else{
echo "Offline";
}
然后当他们点击退出或cookie /会话已过期时:
$update = mysql_query("UPDATE table SET status='offline' WHERE id='$userid'")
or die(mysql_error());