mysql 用户在线判断,什么是确定用户是否在线的最简单方法? (PHP / MYSQL)

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

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.

EDIT: 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.

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."

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).

Constantly Updated

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值