CodeIgniter Forums
How to track Who's Online - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to track Who's Online (/showthread.php?tid=13333)



How to track Who's Online - El Forum - 11-19-2008

[eluser]Unknown[/eluser]
Hi, i have some different type of user and like as pro and learner. I want to track out who is online from those who are pro and who is online from learner. In code igniter how can i do it. I give the user_id in session when it log in.

plz help me

Best Regards
Behestee


How to track Who's Online - El Forum - 11-19-2008

[eluser]davidbehler[/eluser]
Actually you can't track in real time who is online and who is not (well...you could do that using javascript but let's say you only want to use php and mysql) but you can track who was online in the last 5 minutes. All you need is a column in your user table of the type timestamp or datetime that you update each time the user logs in/moves on your site.

And then you can use something like this to determine who has been online in the last 5 minutes:
Code:
SELECT * FROm user_table WHERE DATE_ADD(datetime_column, INTERVAL 5 MINUTE) >= NOW();

As far as I know that (or something similar) is how it's done most of the time/how I usually do it.


How to track Who's Online - El Forum - 11-19-2008

[eluser]rogierb[/eluser]
Since you have the session and the user_id, it shouldn't be that difficult.

use something like
Code:
select GROUP_CONCAT( user_id ORDER BY user_id DESC SEPARATOR ',' )as user_ids from session_table

To get all the user_ids => $user_ids.

Then do something like
Code:
select role, count(role) from user_table where user_id in ( $user_ids ) group by role

This should leave you with 2 records, one for each role.


How to track Who's Online - El Forum - 02-20-2009

[eluser]bd3521[/eluser]
Something like this should also work - assuming you have a userdata set (with db sessions on) for the key "username"

select * from ci_sessions where user_data like '%username%' order by last_activity desc;
Then just extract the username