1

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

sohaan
  • 637
  • 1
  • 10
  • 18
  • 1
    This thread should help you. http://stackoverflow.com/questions/675913/looping-through-all-a-servers-sessions-in-php – msponagle Apr 26 '12 at 16:01
  • This too: [Show Number of Live Page Viewers](http://stackoverflow.com/questions/10113561/show-number-of-live-page-viewers/10113927#10113927) – Blake Apr 26 '12 at 16:04
  • You can't determine whether a user is looking at another tab, or has their browser window minimised, afaik - where you've seen this elsewhere, it really is just a count of authenticated users who've visited the site in the last X minutes (see Kolink's reply). – halfer Apr 26 '12 at 16:05
  • @halfer check this http://stackoverflow.com/questions/1760250/how-to-tell-if-browser-tab-is-active – sohaan Apr 26 '12 at 16:13
  • @sohaan - interesting. I had pondered if that was possible with JS, but I felt it complicated the answer for you, since in general "current user count" systems don't bother with that stuff. In any case, to make it correct, you'd need to report this back to the server via AJAX every so often - a wasteful extra load on your server and bandwidth - which is why people don't do this unless they have good reason to. In their case afaict, it is to do foreground JS processing. – halfer Apr 26 '12 at 16:22
  • @halfer i need to have exact number of users online as i will be using this to let users chat with each other so i need be 100% accurate with number of people online so i think i will have to sacrifice little bandwidth for perfection – sohaan Apr 26 '12 at 16:32
  • "let users chat with each other" - ah, a detail that might have been useful in your post! Btw, just so you know, it is always a good idea to do some research first (i.e. some thorough searching) before asking, so people can see you've made a prior effort. It helps avoid downvotes `;)`. – halfer Apr 26 '12 at 16:46
  • @halfer Sir i did my research on stackoverflow itself but all of those were not to the mark as per my specific requirements so i put my question with my specific requirements – sohaan Apr 26 '12 at 17:41

2 Answers2

11

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.

Shoe
  • 74,840
  • 36
  • 166
  • 272
1

Almost all sites that show users online actually show "users online within the last X minutes". Just keep track of the last time each user loaded a page, and count how many users did that in the last X minutes (X being whatever you want to consider "online")

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592