Counter script

From: russdh (russdh_at_hotmail.com)
Date: 09/30/04


Date: Thu, 30 Sep 2004 13:52:29 +0000 (UTC)

Hi

I'm pretty new to PHP scripting and I've been messing around with a very
simple counter script. Everything works fine apart from the occasional reset
of the counter file. I realised that this could be caused by 2 or more users
accessing this file at the same time and thought about using the flock()
procedure somewhere in the script but I'm not too sure how it should be
implemented. The code is below.

<?

$log = 'counter/counter.txt';

$output_str = "";

// Open log file, read log file value, increment value, write to logfile,
close.

$open=@fopen($log,'r+');

$counter=@fread($open,filesize($log));

@fclose($open);

$counter++;

$write=fopen($log,'w');

fputs($write,$counter);

fclose($write);

// Format numbers on page along with leading zeros.

$leading = 7;

$len_str = strlen($counter);

$leading_zeros = ($leading - $len_str);

for($y=0;$y<$leading_zeros;$y++){

   $output_str = $output_str . "<img src=\"counter/0.gif\" height=20
width=15 ALT=\"0\">";

}

for($i=0;$i<$len_str;$i++){

   $digits = substr($counter,$i,1);

   $output_str = $output_str . "<img src=\"counter/$digits.gif\" height=20
width=15 ALT=\"$digits\">";

}

echo ("<p align=center><font class=f7>You are visitor
number</font><br><table cellpadding=0 cellspacing=0 width=105 height=20
border=1><tr><td width=105 height=20>" . $output_str .
"</td></tr></table></p>");

?>

Any help would be greatly appreciated.

Many thanks.

Russ.



Relevant Pages

  • Counter and statistical checking scripts please?
    ... Can anyone recommend a counter script to track unique visitors? ... tit in osCommerce, since their counter is pretty useless, counting each and ... statistical data that's useful, ... I also want to write this data to a database, if possible, so script advice ...
    (alt.php)
  • Counter and statistical checking scripts please?
    ... Can anyone recommend a counter script to track unique visitors? ... tit in osCommerce, since their counter is pretty useless, counting each and ... statistical data that's useful, ... I also want to write this data to a database, if possible, so script advice ...
    (comp.lang.php)
  • Re: Counter and statistical checking scripts please?
    ... > Jme wrote: ... >> Can anyone recommend a counter script to track unique visitors? ... >> statistical data that's useful, ... >> I also want to write this data to a database, if possible, so script ...
    (comp.lang.php)
  • Re: Counter and statistical checking scripts please?
    ... > Jme wrote: ... >> Can anyone recommend a counter script to track unique visitors? ... >> statistical data that's useful, ... >> I also want to write this data to a database, if possible, so script ...
    (alt.php)
  • Is script type="text/perl" OK?
    ... I've asked this on a html forum but I'll ask it here also. ... I have a counter file on my index page which is like below. ... The script is is a perl file which is processed by the server. ... Since the server (not the client browser) is doing the running of the ...
    (comp.lang.perl.misc)