Page 1 of 1
Track user existence
Posted: Fri Dec 14, 2007 2:08 am
by jasvarghese
Hi all,
i am working on a program basically with individual user area and interfaces. The administrator need to see online users in his area. I was trying with setting up a flag in database while user logs in or out. But this wont work if the user closes the window. So i must find something to check frequently the existence of the users.
I heard of php sessions which can do frequent checks.. CAn anybody give some details about it?
What would be the best possible way to do this? Can anyone refer me some articles which describing this?
All the helps would be greatly appreciated.
Thanks in Advance...
Posted: Fri Dec 14, 2007 7:44 am
by waradmin
On the bottom corner of my site I am able to see how many users are online based on sessions:
I do something like:
Code: Select all
function getOnlineUsers()
{
if ( $directory_handle = opendir( session_save_path() ) )
{
$count = 0;
while ( false !== ( $file = readdir( $directory_handle ) ) )
{
if($file != '.' && $file != '..')
{
if(time()- fileatime(session_save_path() . '/' . $file) < MAX_IDLE_TIME * 60)
{
$count++;
}
}
}
closedir($directory_handle);
return $count;
}
else
{
return "0";
}
}
$online = getOnlineUsers();
That being said, it doesn't track to see if only members are on, however you could likely modify it to do such by setting a different session value. The above is from an article but I can't find it again, I hope this at least gets you started.
Posted: Fri Dec 14, 2007 10:13 am
by spadmore
You should use sessions for this . Maybe create an include file to be used in every page that updates a session stored array which keeps the list of usernames..
- Shelon Padmore
Posted: Fri Dec 14, 2007 1:44 pm
by aliasxneo
Probably best to track this information either in the database or a flat file.
If it were me creating this system I would do it as such:
Create a separate field in your "members" table which will hold the last time the user visited your website. I would simply use something like time() to log this. Then create a small script which queries the DB to find out what users have visited in the past 5 or so minutes.
Code: Select all
$now = time();
$sql = "SELECT * FROM `members` WHERE ({$now} - `lastvisit`) < (5 * 60)";
Essentially what that does is gets the number of seconds that has passed since the last visit and then checks if it's less than 5 * 60 (5 minutes) and if so it would return that row, meaning that user has been on in the last 5 minutes. You can of course modify the exact time if you wish.
Posted: Fri Dec 14, 2007 2:49 pm
by John Cartwright