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...
Track user existence
Moderator: General Moderators
On the bottom corner of my site I am able to see how many users are online based on sessions:
I do something like:
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.
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();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.
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.
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)";- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact: