How to get the count of online users using php

Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to get the count of online users using php

Post by Neo » Tue Feb 23, 2010 12:16 am

Put this code to every file you need to count online users.

Code: Select all

<?php
$file= 'ip.txt'; //file to store data
$every=20;  //start counting from 0 after these many minutes

//no need to edit below
$ip = $_SERVER['REMOTE_ADDR'];
$a= file_get_contents($file); $a=explode("\n",$a);
if(is_numeric($a[0])){
     if((time()-$a[0])>($every*60)){
          $st= time()."\n".$ip;
          $fp=fopen($file,'w'); fwrite($fp,$st); fclose($fp);
     }
     else{
          if(!in_array($ip,$a)){
               $st= "\n".$ip;
               $fp=fopen($file,'a'); fwrite($fp,$st); fclose($fp);
          }
     }
}
else{
     $st= time()."\n".$ip;
     $fp=fopen($file,'w'); fwrite($fp,$st); fclose($fp);
}
echo '<p>Total users online: '. (sizeof($a)-1). '</p>';

?>
Post Reply

Return to “PHP & MySQL”