CREATING A SIMPLE PHP HIT COUNTER - Aug 2nd, 2010


For each page that you want to count hits on, create a text document with the number that you want your counter to start
with, and a unique file name, like: aboutpagecounter.txt and upload it to your site.

I usually insert the number 500 in my text files, so that my counters will not display a really small number at first,
but you can start them with any number you want to.

If you have many separate counters, you might want to create a folder for your counter text files.

Then somewhere in the body of the page that you want to count, insert this code, changing the file name and path to suit
your particular situation:



<?php
$count_my_page = ("aboutpagecounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);?>


Then, where you want your hit count to appear, insert this line of code:



<?php echo $hits[0]; ?>


You can, of course style your counter any way you want to.



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service