I’m trying to count currently active users on my page, without storing any visitors in my database. So I wonder if anyone has used this function I found at devarticles about how to count active sessions (http://www.devarticles.com/art/1/215/1), with good result, because it don’t seem to work for me.
There is an error in the code (missing an } ) so I’ll post the code here (corrected) if you’re not up to view that page.
/* Start the session */
session_start();
/* 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) < MAX_IDLE_TIME * 60) {
$count++;
}
}
closedir($directory_handle);
return $count;
}
} else {
return false;
}
}
echo 'Number of online users: ' . getOnlineUsers() . '<br />';
You can change this line
if ($file != '.' && $file != '..')
to
if (is_file($file) )
if you like to.
The error I get is that my $file only contains a “.” , does anyone know what may be the error?
/Charlotte