Welcome Guest, Not a member yet? Register   Sign In
Number of users online
#1

[eluser]mvdg27[/eluser]
Hi,

For a project I'm working on now, I'd like to add a 'number of users online'-feature. I was reviewing the Session-class, but didn't find a direct answer on how to do such a thing with CI. Can anyone give me some pointers on how to do that?

Thanks! -Michiel
#2

[eluser]xwero[/eluser]
whos.amung.us and many others offer widgets with possibilities you will take some time to develop.
#3

[eluser]Pascal Kriete[/eluser]
What you would have is a table called Users, where you save the usernames along with a timestamp.

When a page is loaded you call something like this (pseudo-code):
Code:
function updateCurrentUsers() {
    $curr_time = time();
    $allowed_timeout = $curr_time - 58490385; //make this smaller ;)
    
    /* Add current user to the database with timestamp field = $curr_time() */

    /* Select all users where timestamp field < $allowed_timeout, and delete them */
    /* Return all users left in the table */
}
#4

[eluser]mvdg27[/eluser]
Hi inparo, thanks for your solution. That seems like a good option.

Cheers!
#5

[eluser]mironcho[/eluser]
If you are saving session data into database (like described in http://ellislab.com/codeigniter/user-gui...sions.html), then you could use last_activity field to calculate online users. So if you want to find online users for last 10 minutes, you could try this:

Code:
$query = $this->db->query("SELECT COUNT(*) AS online_users FROM ci_sessions WHERE last_activity > (UNIX_TIMESTAMP() - 600)")

Note: last_activity is updated on every five minutes!
#6

[eluser]mvdg27[/eluser]
That's even better!! "last_activity is updated on every five minutes!" .. that means there is a five minute error of displaying the correct number of online users, right? If so, that shouldn't be a problem as it's meant as a gadget anyway.

Thanks
#7

[eluser]mironcho[/eluser]
That's right!
#8

[eluser]tonanbarbarian[/eluser]
you could just do the count of records in the session table if you want an approximation.
session gc (garbage collection) will eventually delete these old records anyway
a simple count on the entire table is quicker than having to count with the where (marginally)
#9

[eluser]mironcho[/eluser]
tonanbarbarian, you are right, but by default in codeigniter session expiration time is set to 2 hours so counting all sessions will return online users for last 2 hours.




Theme © iAndrew 2016 - Forum software by © MyBB