Couting the connected users

Posted on 2009-02-09
Medium Priority
Last Modified: 2012-05-06

I am building a PHP application and I want to see how many users are connected to my application and what is their IDs.

I can do this using by storing session information on file and/or DB but I need to consider the case where user just closes the window without logging off or there are countries in the world where the internet connection in not good and they also face electricity breakdown.

I think if i could have some thing link Application in ASP where we can store shared information of an application.

Any idea or help will be appreciated.
Question by:Shahzad Fateh Ali
  • 2
LVL 10

Expert Comment

ID: 23589384
What you do is you add a timestamp with this information in the table. Then, every time the user visits a page, you update this timestamp. Then, when you display the number of users that are online, you first check whether any records are older than e.g. 5 minutes. Something like this:

delete from online_users where last_visit < /* 5 minutes ago */

Then, your online users table is always up to date.

Author Comment

by:Shahzad Fateh Ali
ID: 23589504
But, what if a user remains on same page for more thn 15 mins or so, without any activity?
LVL 10

Accepted Solution

pvginkel earned 1500 total points
ID: 23589531
Well, you have to make a choice. You can only see whether a user is online when the user actually requests a page. I do not know how important this statistic is for you, but if it's just nice to have information, you shouldn't worry about this.

If on the other hand this information is critical to you, you can always implement something using AJAX that does a request every three minutes. You'd start a timer when the pages loads so that after three minutes, it requests a specific page that signals the server that the user is still there. If the user goes to a different page before the three minutes, the timer would just start counting to three minutes again.

The problem with this solution is that you have more requests to your server then necessary, so it puts more load on your server. But it certainly is a way to go.

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this article, we’ll look at how to deploy ProxySQL.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question