Site hit counter problem

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

User avatar
ppera
New php-forum User
New php-forum User
Posts: 11
Joined: Thu Jan 09, 2003 2:11 pm

Site hit counter problem

Postby ppera » Fri Jan 17, 2003 7:59 am

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

User avatar
WiZARD
Moderator
Moderator
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

Re: Site hit counter problem

Postby WiZARD » Sat Jan 18, 2003 2:35 am

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 ... s0.4.0.zip

User avatar
ppera
New php-forum User
New php-forum User
Posts: 11
Joined: Thu Jan 09, 2003 2:11 pm

Postby ppera » Sun Jan 19, 2003 12:25 pm

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.


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest