Hello,
I am using a very basic hit counter script that just uses a flat .txt file but is a unique hit counter. the problem is that every once in awhile the flat file recording the IP's is resetting itself. Does anyone know how I can prevent this or what may be causing it?
Code:
<?php //hitcounter.php adapted from http://www.phpfreaks.com/quickcode/code/77.php
//Core Variables
$filename = "hitcounter.txt" ;
$startdate = "February 7, 2005" ;
$ip = getenv("REMOTE_ADDR") ;
$currentip = "$ip";
$file = file($filename);
$file = array_unique($file);
//This is the guts of the counter
$file = "$filename";
$fd = fopen ($file, "r");
$fget= fread ($fd, filesize ($file));
fclose ($fd);
$totalips = htmlspecialchars($fget);
if (preg_match ("/$ip/i", "$totalips"))
{$file = file($filename);
$file = array_unique($file);
$hits = count($file);
echo "<b>Visitors since, ";
echo "$startdate:</b><font color=red> $hits</font>";}
else
{
$fd = fopen ($filename , "r");
$fstring = fread ($fd , filesize ($filename)) ;
fclose($fd) ;
$fd = fopen ($filename , "w");
$fcounted = $fstring.$currentip."\n";
$fout= fwrite ($fd , $fcounted);
fclose($fd);
$file = file($filename);
$file = array_unique($file);
$hits = count($file);
echo "Visitors since,";
echo "$startdate: $hits";
}
?>