How to make a Hit Counter using PHP

Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to make a Hit Counter using PHP

Post by Neo » Sun Feb 21, 2010 3:23 pm

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) ;

?>
Post Reply

Return to “PHP & MySQL”