用户在线离线状态-离线状态问题 [英] User online offline status - offline status issue

查看:251
本文介绍了用户在线离线状态-离线状态问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这是 PHP:离线状态的相关问题 但是由于我的方法没有什么不同,而且也有所不同,所以我认为新的问题会更好.

First this is related question of PHP: Online Offline Status But as my approch is little different and issue too so I thought new question would be better.

现在,我在qa_users表中添加了一个称为在线"的列,该列可插入/更新日志记录时间并在用户交互时保持更新(我不确定是否正确)

Now I have added one column to qa_users table called "online" which insert/update logging time and keep update upon user interaction (i am not sure is it right or not)

现在,它在用户登录后立即显示在线,但问题是在注销后仍保持显示状态为在线,而从未处于脱机状态.

Now it is displaying user online as soon as they logged in but issue is after logged out they are keep display status as online and never go offline status.

我认为我的时间比较条件代码有问题,但是我不知道.

I think something wrong with my time comparison conditional code but what I don't know.

请在下面的代码中找到我正在使用的代码.

Please find below code what I am using.

$loggedtime = date('Y-m-d h:i:s', time()-300); // 5 minutes

$query = 'SELECT userid, handle, online FROM ^users ORDER BY userid ASC';           
$result = qa_db_query_sub($query);  

while($ids = mysql_fetch_array($result)){

    $online = $ids['online'];
    $userid = qa_get_logged_in_userid();

    if(qa_is_logged_in()){
        $update = 'UPDATE ^users SET online = NOW() WHERE userid = '.$userid.'';

        qa_db_query_sub($update);
    }

    $time = $ids['online'];

    if ($time >= $loggedtime){ // i have tried with $loggedtime > $time too
        echo '<li>'.$ids['handle'].' online</li>';
    } else {
        echo '<li>'.$ids['handle'].' offline</li>';
    }               

}

推荐答案

在不知道您的数据库结构的情况下,这只是一种猜测.

Without knowing your DB structur it's kind of a guess.

if ($time >= $loggedtime)

您正在将类似'2012-11-01 10:10:10'的字符串与数据库中的$time进行比较.这似乎是这里的问题. 您可以/应该使用UNIX时间戳.可以轻松比较它们.

You are comparing a string like '2012-11-01 10:10:10' with whatever $time is in your DB. This seems to be the problem here. You could/should use UNIX timestamps. They can be compared easily.

如果$time是UNIX时间戳,则可以执行以下操作:

If $time were a UNIX timestamp you could just do:

if ($time >= time()-300)

更改查询以获取online

$query = 'SELECT userid, handle, UNIX_TIMESTAMP(online) as online FROM ^users ORDER BY userid ASC';

为了更清楚一点: 在您的第一个版本中,您正在以'2012-11-01 10:10:10'的形式比较两个日期.

To make it more clear: In your first version you were comparing two Dates in the form '2012-11-01 10:10:10'

if ('2012-11-01 10:10:10' < '2012-11-02 10:10:10')

这在PHP中不起作用-就像这样做:

This can't work in PHP - it's like doing:

if ('apples' < 'bananas')

您必须比较数字.因此,我建议使用可以轻松比较的unix时间戳.

You have to compare numbers. Therefore i suggested using unix timestamps which can be easily compared.

这篇关于用户在线离线状态-离线状态问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆