Site hit counter problem

A

Anonymous

Guest
I have on my site hit counter with following code as include file. It is accessed from 5 page, traffic is about 4000-5000 hits per day. But sometimes counter goes to low value, couple hunderts, it looks that it resets self. It happened 5 times in last 4 months, but 2 times in last 4 days.

Code:

<? $countfile = $DOCUMENT_ROOT.'/counter/visits';

$couns = "1";

if (file_exists($countfile)) {

$fd = fopen ( $countfile ,"r") ;
$count = fread ($fd, 7);
fclose ($fd);

$counti = intval ($count) ;
$counti = $counti+1;

$couns = strval($counti); }

$count7 = str_pad($couns, 7, "0", STR_PAD_LEFT); #Add leading zeros by need

$fd = fopen ( $countfile ,"w") ;
$fwr = fwrite ($fd, $count7);
fclose ($fd); ?>


Can it be made more reliable (I assume that multiple access causes trouble). Or I need to use separated counter for each page (but then one page can have multiple access too....) ?
 
If visitors of your site near 200 fnd more and you nont want problem with Counter system use MySQL.
or get this:
http://216.239.35.120/translate_c?hl=en&u=http://www.php-resource.net/index.php%3Fref%3DDownload%26file%3DAllMyVisitors0.4.0.zip
 
Thanx Wizard. I downloaded it, but I want something really simple.
Anyway, I modified code, so no file creation if FOPEN from some reason not opens file. I suspect that problem was that when server is overloaded it can't open file and then creates new, with counter value set to 1.
 
Back
Top