什么是确定用户是否在线的最简单方法? (PHP / MYSQL) [英] Whats the easiest way to determine if a user is online? (PHP/MYSQL)

查看:325
本文介绍了什么是确定用户是否在线的最简单方法? (PHP / MYSQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让我可以通过会话来了解用户是否在线?

Is there a way I can piggy back sessions to know if the user is online?

即:使用登录,我设置$ _SESSION变量,用户次数out-cookie垃圾收集器更新数据库以将其状态更新为脱机。

I.e: use logs on, I set a $_SESSION variable, user times out- cookie Garbage collector updates the database to update their status as offline.

编辑:我想要一个解决方案不涉及时间或日期。我想要一些东西可以骑在会话或类似的东西上。猜测某人是否在线并不足以满足我的需求。

I want a solution that does not involve times or dates. I want something to ride on sessions or something similar. Guessing if someone is online is not good enough for what I need.

推荐答案

不要费心去弄清楚时区之间的差异。这不是必需的。

Don't bother with figuring out the differences between timezones. That's not necessary.

每当用户访问页面时,更新其用户表last-updated-time记录中的字段。然后对最近5分钟内具有最后更新时间的所有用户进行查询。除此之外,它们被认为是离线的。

Whenever the user accesses a page, update a field in their record of the Users table last-updated-time. Then do a query for all users having a last-updated-time within the last 5 minutes. Anything more than this, and they are considered "offline."

如果您使用服务器时间,通过MySQL中的NOW()函数,您将支持 - 计算时区之间差异的步骤。

If you use your server-time, via the NOW() function in MySQL, you'll side-step calculating differences between timezones.

这是跟踪目前有多少用户在线的标准方式(意思是,在最后几分钟内有效)。

This is the standard way of tracking how many users are presently online (Meaning, active within the last couple of minutes).

如果你想知道他们仍然活跃,即使他们没有从一个页面跳到另一个页面,包含一些javascript来每隔60秒左右ping你的服务器,让你知道它们还活着。它的工作方式与我原来的建议相同,但它会更新您的记录,而不要求他们至少每五分钟疯狂浏览一次您的网站。

If you would like to know they are still active even when they're not jumping from page to page, include a bit of javascript to ping your server every 60 seconds or so to let you know they are still alive. It'll work the same way as my original suggestion, but it will update your records without requiring them to be frantically browsing your site at least once every five minutes.

var stillAlive = setInterval(function () {
    /* XHR back to server
       Example uses jQuery */
    $.get("stillAlive.php");
}, 60000);

这篇关于什么是确定用户是否在线的最简单方法? (PHP / MYSQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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