A hit counter will let us know how many times a page is accessed. Incase one visitors loads the page several times, the hit counter willincrease several times (but this is likely to happen only a few times).
The code for the hit counter bellow will save the number of hits in afile named counter.txt(the name of this file may be changed). Each time the page is loaded,the file will be read, the number will be increased by one and the newnumber will be saved to the same file.
Counter.php
In case you want to use different hit counters for different pages,chage name of $counterfile for each page (p.e.: counter1.txt,counter2.txt, etc).
The code for the hit counter bellow will save the number of hits in afile named counter.txt(the name of this file may be changed). Each time the page is loaded,the file will be read, the number will be increased by one and the newnumber will be saved to the same file.
Counter.php
<?php
//The file where number of hits will be saved; name may be changed;p.e. "/counter_files/counter1.txt"
$counterfile = "counter.txt";
// Opening the file; number of hit is stored in variable $hits
$fp = fopen($counterfile,"r");
$hits= fgets($fp,100);
fclose($fp);
//increading number of hits
$hits++;
//saving number of hits
$fp = fopen($counterfile,"w");
fputs($fp, $hits);
fclose($fp);
//printing hits; you may remove next line (and keep the counteronly for your records)
print $hits;
?>
To use this code, copy itto your page in the exact position where you want to show number ofhits. If your page is a ".html" page, change the extension to ".php".Them, visit your page. In the first visit, you will get a couple oferrors (the file where number of hits are recorded does not exists, sosome errors will be shown in the page). Reload the page and you willsee no errors. The hit counter will be working. //The file where number of hits will be saved; name may be changed;p.e. "/counter_files/counter1.txt"
$counterfile = "counter.txt";
// Opening the file; number of hit is stored in variable $hits
$fp = fopen($counterfile,"r");
$hits= fgets($fp,100);
fclose($fp);
//increading number of hits
$hits++;
//saving number of hits
$fp = fopen($counterfile,"w");
fputs($fp, $hits);
fclose($fp);
//printing hits; you may remove next line (and keep the counteronly for your records)
print $hits;
?>
In case you want to use different hit counters for different pages,chage name of $counterfile for each page (p.e.: counter1.txt,counter2.txt, etc).
By phptutorial.info
Yeah adding hit counter to your site is very important.It not only reports your traffic numbers, but also gives you valuable information that will help you understand the behavior of your site visitors better and so perform outstanding customer intelligence.It helps you rediscover your site and see it with the eyes of your visitors. Simple hit counter tools are available in order to help you track the activities of customers instantly without any hassle.
ReplyDeletehttp://counterforsite.com/simple-hit-counter/