Track user existence

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasvarghese
Forum Newbie
Posts: 3
Joined: Fri Aug 24, 2007 2:30 am

Track user existence

Post 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...
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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.
spadmore
Forum Newbie
Posts: 2
Joined: Fri Dec 14, 2007 10:05 am

Post 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
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Ask and you shall receive.

viewtopic.php?t=34665&highlight=xmlhttp
Post Reply