Track Users Online with Sessions?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Since I'm on the topic of sesions, I've seen a few examples of tracking
online users using a mysql table and inserting the session and
timestamp and checking it at period to remove it from the list after
expiration time but is there a simpler way?  Is there a way to count
how many active sessions there are open at one time?  I don't care to
see user info but would like to at least know how many people are
online.  I guess getting logged in users and guests would require using
a table or something.  Is there any information on how to do this?

Re: Track Users Online with Sessions?

I found this and is what I'm looking for but no matter what I get '0'
for the count every time.

/* Start the session */

/* Define how long the maximum amount of time the session can be
inactive. */
define("MAX_IDLE_TIME", 3);

function getOnlineUsers(){

if ( $directory_handle = opendir( session_save_path() ) ) {
$count = 0;
while ( false !== ( $file = readdir( $directory_handle ) ) ) {
if($file != '.' && $file != '..'){
// Comment the 'if(...){' and '}' lines if you get a significant amount
of traffic
if(time()- fileatime(session_save_path() . '\' . $file) <

return $count;

} else {
return false;


echo 'Number of online users: ' . getOnlineUsers() . '<br />';

Site Timeline