A simple, yet flexible hit counter for web pages

Web page counters can be popular, but tend to be tricky to set up, and in many cases cause problems if the counter program is located on a different site than the page being counted. Counters can also slow web file transfer if a file must be accessed remotely or an image created on-the-fly. They can also interfere with cacheing of pages slowing retrieval of the same information and leading to net congestion. As a general rule, the writer of a web document is more interested in the access count than the reader. This document describes a simple way to create a counter which overcomes many of these problems that you can incorporate into your web page.


This counter script and examples are written for the directory structure on west.net. You may need to alter the directory and path information for use on another server. The script may not work at all if your server does not allow user-defined CGI scripts. If you are using an ISP other than west.net and the counter does not operate, ask the web administrators at your site for help.

The Script

This counter uses a shell script that must reside in your cgi-bin directory. The script creates a file the first time it is run, containing the number 1. Each subsequent time the script is run it looks for that file, gets the number, and increments it by one.

The script is called by your HTML code in the page being counted, and the output file is named there. Because you control the name of the file that is created with the count, you can use the same script to count multiple pages just by specifying a different output file. This also allows you to cheat. Here's the script, which can also be retrieved by FTP.


# This script creates a file showing the number
# of times it has been executed.  Useful for 
# background counts of html accesses and the like.
# Written by Jay Hennigan (jay@west.net)
# Released to the public domain 1996.

typeset -i counter=1
if test -s $outfile 
  read counter < $outfile
  echo $counter > $outfile
You will need to place the above code into a directory labeled
immediately under your home directory. You can either The count script file and the cgi-bin directory must both be executable in order to function.

Calling the script from the page to be counted

Once you've created the counter script, you now need to cause it to be run when a page to be counted is viewed. This is done by the following line inserted in the page:

<!--#include virtual="/cgi-bin/cgiwrap/username/count?filename"-->

Substituting your own username for username above, and also substituting a filename such as page.hits for filename above. If you're counting multiple pages, each one should have a unique filename.

For example, the code that counts this page is:

<!--#include virtual="/cgi-bin/cgiwrap/jay/count?countpage"-->

You also need to make the page to be counted executable. To do this, run the following unix commands, assuming that the page to be counted is page.html .

cd public_html
chmod 755 page.html

Displaying the count


It isn't necessary to display the count at all, bear in mind that such displays will slow down the access to your page, and may not be accurate if the page is cached. As a rule the count is more interesting to you as the author than to the reader. If you choose not to display the count, you can always go to your public_html directory and read the file page.hits .

To display the count on a web page, we can include the file in a line of text, like this:

The page page has been accessed <!--#include file="page.hits"--> times.

For this page, this yields:
The page you're reading has been accessed 1708 times.

Created by Jay Hennigan
Questions, comments, flames?