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....) ?
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....) ?