Display number of curent online visitors

GZ - online visitors counter


<?php

// two config lines
$dataFile = "counter.txt";
$sessionTime = 5; //this is the time in **minutes** to consider someone
online before removing them from our file

if(!file_exists($dataFile)) {
$fp = fopen($dataFile, "w+");
fclose($fp);
}

$ip = $_SERVER['REMOTE_ADDR'];
$visitor = array();
$onvisitor = array();

// insert new in counter.txt
$fp = fopen($dataFile, "r");
flock($fp, LOCK_SH);
while(!feof($fp)) {
$visitor[] = rtrim(fgets($fp, 32));
}
flock($fp, LOCK_UN);
fclose($fp);

// removing old entries
$x = 0;
$old = FALSE;
foreach($visitor as $key => $data) {
list( , $last) = explode("|", $data);
if(time() - $last >= $sessionTime * 60) {
$visitor[$x] = "";
} else {
if(strpos($data, $ip) !== FALSE) {
$old = TRUE;
$visitor[$x] = "$ip|" . time();
}
}
$x++;
}

if($old == FALSE) {
$visitor[] = "$ip|" . time();
}

//writing
$fp = fopen($dataFile, "w+");
flock($fp, LOCK_EX);
$i = 0;
foreach($visitor as $single) {
if($single != "") {
fwrite($fp, $single . "\r\n");
$i++;
}
}
flock($fp, LOCK_UN);
fclose($fp);

echo '<strong>curently ' . $i . ' online</strong>';

?>

demo ~61 KB comments (0)

User Comments (0)

( ADD YOURS ) ( GET GRAVATAR )
  1. 31.12.1969

    ( Reply )

Notice: Undefined index: gotovo in /home/content/g/u/t/gutovic/html/full.php on line 249

 

« Older posts