douba9776 2012-04-26 15:58
浏览 26
已采纳

如何显示当前有多少用户在线[关闭]

I need to show my users other users who are currently online i could have maintained a log of login and logout and based on that i could count total people online but many a time users don't log out and so my count would not be valid .

Here are criteria to show that user is online in:

  1. user has just logged in

  2. user is currently viewing the page

  3. user has the left tab open but currently browsing in another tab

  4. use has tab open but has minimized the web browser

Here are criteria when use should not be shown online

  1. user has logged out;

  2. user dosent wants to be shown online

  3. user closed the browser with tab was open

  4. user closed the browser

I dont know how to go ahead with this. I am using php on server side and mysql and mongo db as databases please help me with this ...

Thanks in advance

  • 写回答

2条回答 默认 最新

  • dtyz76562 2012-04-26 16:32
    关注

    There's just a method to be somehow sure that a user is "online". To accomplish it you need to understand that with "online" people usually refer to an user being online in that moment; while checking this is not easy (or even not possible), to check if a user has made any action within an x amount of time is very easy, and it's the choice most of the programmers do.

    It's pretty easy:

    1. You create a field for each logged in user in a database called last_activity.
    2. Every time a user visit a page you set last_activity to the current time()stamp.
    3. You define a $x amount of seconds that a user can be idle (not browsing any new page) before being considered offline. (Usually it is 15 min or 30 min or 1 hour).
    4. You do a simple query like "SELECT * FROM users WHERE last_activity >= ". (time() - $x)
    5. You now have a list of users that has loaded a page within $x seconds.

    Unlike other things, in PHP and with web browser there's no way to detect for sure when a user has closed one of your page in the browser, so it's technically impossible to know whenever a user has closed the browser window or exit your website.

    By the way, I lied, there's a method but it's really expensive so please think a lot about it because it could crash your server or something like that:

    1. Create the last_activity field in the user table (as below).
    2. Create a Javascript code (using AJAX) that every y seconds require update.php (just an example).
    3. Put that javascript in every page of your website (Consider y to be the bigger number you can (< 30 seconds may be bad)).
    4. Create update.php where you update last_activity with the current time()stamp.
    5. Do the same query as before using $x = $y + 1 when you want to find out the online people.

    With the latest solution you can be sure that whenever a user is not on the page anymore, he is also not running the AJAX call and then within y seconds it will be not considered online anymore. It's a very similar solution to the first one, but you can set y to be smaller than the previous x. In this way you have more informations about the current state of the browser window of the user but you have much more database load than before.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用