Couting the connected users

Posted on 2009-02-09
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
    LVL 10

    Expert Comment

    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.
    LVL 9

    Author Comment

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

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now