This blog is NOFOLLOW Free!
  • INCEPTION THEME

    Clean, contrasting WordPress theme built on the foundation of Whiskey Air.
  • WHISKEY AIR THEME

    Clean developer theme, suited for heavy modifications.
  • X5 TURBO THEME

    3 columns Turbo theme, now reloaded!
  • CLEAR APPLE THEME

    Almost magazine theme, without the hassle of administration.

If, for any given reason, you are not able to implement a MySQL counter, here is the easiest solution for creating a flat file online users counter. Create a file called online.php, make it writable (either CHMOD 755 or CHMOD 777, depending on your server requirements), and insert the following code where you want the counter to appear.

Here is the code:


<?php
$file_name = "/how_to_build_an_online_visitor_counter/online.html";
$c_time = time();
$timeout = 60;
$time = $c_time - $timeout;
$ip = getenv("REMOTE_ADDR");

if(!file_exists($file_name)) {
$fp = fopen($file_name, "w");
fwrite($fp, " \n");
fclose($fp);
}

$fp = fopen($file_name, "a");
$write = $ip."||".$c_time."\n";
fwrite($fp, $write);
fclose($fp);

$file_array = file($file_name);
$online_array = array();
for($x = 1; $x < count($file_array); $x++) {
list($ip, $ip_time) = explode("||", $file_array[$x]);
if($ip_time >= $time) {
array_push($online_array, $ip);
}
}

$online = array_unique($online_array);
$online = count($online);
if($online == "1") {
print "$online user online";
}
else {
print "$online users online";
}
?>

I have successfully used this code on many sites for years without errors. So, trust me on this!

Photo credit by ja1vbn

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>