Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

Design Question: how to show number of users online?

2

43 views

Planning to develop a LAMP web application. What general strategies can I use to display the number of users currently logged in to my site? I want to be able to accurately display something like, "There are currently 1000 users online" or "User John Doe is currently online".

asked June 21, 2011 3:37 pm CDT
posted via StackOverflow

6 Answers

2
Best answer
 

A database will be involved. So every time someone logs into the site, you can have a field in a user's table for last_login. And then there can be a script that does a query against this user's table to count the number of rows last_login within the last x amount of time. It may be good to cache this and repopulate this cache every z amount of time, and then pull from this cache as oppose to running a query against the user's table every request. So database + some kind of caching system.

answered June 21, 2011 4:23 pm CDT
1
 

This is quite easy to do, but cannot be accurate. Html being stateless, there is no way of knowing if a user is still looking at your page or has left.

If you want to log anonymous and logged in users, you coud use a tracking cookie with a short timeout, say five minutes and have this cookie link to a database of active sessions.

answered June 21, 2011 4:23 pm CDT
0
 
  1. count the number of active sessions in your session db.
answered June 21, 2011 4:23 pm CDT
0
 

If you're using your database to store session information, you can easily query the session table and get how many unique sessions there are stored.

answered June 21, 2011 4:23 pm CDT
0
 

Have some sort of last accessed time record in the DB, and record that the person has been active. Then query the DB for those users who have been active in the last 5mins or so. Will give you a close approx.

answered June 21, 2011 4:23 pm CDT
0
 

I recommend using the CodeIgniter PHP framework.

This will allow you to store your session data in the database very easily (you just enable it in the config.php file). Then you can query the number of session ids in the session table of your database.

Here is the information for the CodeIgniter session class so you can see how to use it: CodeIgniter Session Class

Here is also a link to the CodeIgniter forums going through more detail of how exactly to get this implemented: CodeIgniter Forum

answered June 21, 2011 4:23 pm CDT

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions
Database design question
February 13, 2011