Number of users online |
[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
[eluser]xwero[/eluser]
whos.amung.us and many others offer widgets with possibilities you will take some time to develop.
[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() {
[eluser]mvdg27[/eluser]
Hi inparo, thanks for your solution. That seems like a good option. Cheers!
[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!
[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
[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)
[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. |
Welcome Guest, Not a member yet? Register Sign In |