Page 1 of 1

How to make a Hit Counter using PHP

Posted: Sun Feb 21, 2010 3:23 pm
by Neo
This script will increment a counter by 1 at each time a surfer on the Internet visits your page.

Upload an empty file named as counter.txt and set it to worldwritable (777).
Now add the following code to your html or php file.

Code: Select all

<?php
$filename= "counter.txt" ;
$fd = fopen ($filename , "r") or die ("Can't open $filename") ;
$fstring = fread ($fd , filesize ($filename)) ;
echo "$fstring" ;
fclose($fd) ;

$fd = fopen ($filename , "w") or die ("Can't open $filename") ;
$fcounted = $fstring + 1 ;
$fout= fwrite ($fd , $fcounted ) ;
fclose($fd) ;

?>