Ted's Computer World HTML & CSS
Tips and Tricks

PAGE-HIT COUNTING THE EASY WAY

It you have surfed any websites on this topic, then you have 'learned' the following, among other things:

Well, it doesn't have to be that way.  If all you need is a simple record of the popularity of your various web pages, a little 3-line PHP script will do the job nicely while providing the following benefits:

NOTE: Be aware before continuing that some technical expertise is required.  At least on Apache servers, an appropriate .htaccess file must be placed in every folder containing files to be counted; otherwise the function will fail.  Having little server-side experience myself, I'll not comment on that further; do your own homework.


This sample script logs the page that you currently are reading:

*

That code simply opens a tiny text file, increments the number inside, then closes it.  Later, when you wish to know how many viewers have perused the page, you merely click on that filename and read the number.  What a deal.  Such log files can be placed anywhere, of course; but as is shown, I keep mine in local subdirectories named /pagect.

Yes, all that this procedure actually does is to count the number of times a page is accessed; but that is all that is asked of it.  A date or other information could be included by augmenting the script, in which case you might need to convert your .htm or .html page to a .php page.  I'll leave that to you, because I'm happy with the output as it is.

Note that the sample script has been placed outside the actual body of the page code.  That's my preference, but it really doesn't matter where you put it.  Place the script on any page that you wish to keep track of, edited so as to utilize an appropriately named logfile.

Finally, you can seed any such file with a starting integer value of your choice.  If you leave the file empty, then the counting will start from zero.

That's all there is to it — unless your server does not provide PHP scripting, in which case you should dump that outfit for another one.

Go Back