PHP – How many are online

Here is the script we use at Dracko.com to show how many users are online

function ShowOnline(){
// Timeout – After this time the users will
// be deleted (in minutes)
$timer = 100;
$filename = $_SERVER[‘DOCUMENT_ROOT’]."/usersonline.log";

if (!$datei) $datei = dirname(__FILE__)."/$filename";
$time = @time();
$ip = $_SERVER[‘REMOTE_ADDR’];
$string = "$ip|$time\n";
$a = fopen("$filename", "a+");
fputs($a, $string);
fclose($a);

$timeout = time()-(60*$timer);

$all = "";
$i = 0;
$datei = file($filename);
for ($num = 0; $num < count($datei); $num++) { $pieces = explode("|",$datei[$num]); if ($pieces[1] > $timeout) {
$all .= $pieces[0];
$all .= ",";
}
$i++;
}

$all = substr($all,0,strlen($all)-1);
$arraypieces = explode(",",$all);
$useronline = count(array_flip(array_flip($arraypieces)));
$whoonline = array_flip(array_flip($arraypieces));

// Delete older that $timer*60
$dell = "";
for ($numm = 0; $numm < count($datei); $numm++) { $tiles = explode("|",$datei[$numm]); if ($tiles[1] > $timeout) {
$dell .= "$tiles[0]|$tiles[1]";
}
}

if (!$datei) $datei = dirname(__FILE__)."/$filename";
$string = "$dell";
$a = fopen("$filename", "w+");
fputs($a, $string);
fclose($a);
// display how many people where activ within $timeout
echo "Online:".$useronline;
if($this->user->isLoggedIn() && $this->user->isAdmin()){
$result = array_unique(explode(",",$all)); // get rid of dups(returns array of string $all)
asort($result);
foreach ($result as $value) {
echo "
$value";
if(strcmp($value,$_SERVER[‘REMOTE_ADDR’]) == 0){
echo "*";
}
}
echo "
last ".$timer." minutes";
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *