Web Page Hit Counters

There are undoubtedly many ways to add an access or hit counter to your web page. You know that this topic is quite popular when Yahoo has a whole category devoted to it.

Perhaps one of the most ubiquitous counters on the web is this one:

You can also display Roman Numerals like this:

The examples above are created by cgi scripts that reside in your /cgi-bin directory. To display one on your page, you'll use some HTML similar to the following:

   <img src="/cgi-bin/webcount.cgi?page_name">

The CGI script has a data file which stores hit counts. Whenever it's called, the script will increase the count and spit out a .gif graphic. It may also attempt to eliminate duplicate hits, caused when someone repeatedly hits the Reload button in their web browser.

There are a few disadvantages to using an image-based web counter. Before the count can be displayed, the user's web browser has to make another HTTP connection to retrieve the graphic used for the count. (HTTP/1.1 eliminates the need to establish another connection, but only the most recent web browsers and web servers support this protocol.)

Another problem is that the dimensions of the image are not known to the browser. Notice that in the example above, there are no height= or width= values for the image. In this case, popular web browsers such as Netscape will put off displaying the HTML below the image until the dimensions of the image are known. This means that the page will take longer to be displayed.

For many users, however, the counter graphic can add quite a lot of punch to a web page. They're also very easy to add to any HTML page.

An Alternative

An alternative to the graphic-based web counters are text based ones which appear as part of the HTML document:

54921

To implement a counter like this, you'll need to enable Server Side Includes in your web server. To learn about Server Side Includes (SSI) in Apache, check out these references:

This week's script implements a simple program to count hits on your web pages. To use it, first:

Next, you'll need to make the script executable: Edit the following part of the source code:

# location and name for the log file
$LOGFILE="count.txt";

Change the "count.txt" to include a path to where you want to store the data file. After selecting a suitable location, issue the following commands to make sure the file can be read and written by your web server daemon:

Next, after enabling SSI, add the following code to an HTML file:

<!--#exec cmd="/path/hitcounter.pl page_name"-->

Change the /path to point to the directory where you stored the hitcounter.pl script. You can change page_name to be the name for the current page.

To display a summary of all the page hits, use the following code in your HTML file:

<!--#exec cmd="/path/hitcounter.pl -summary"-->

Then, when you look at the HTML file in your web browser, you'll see something similar to the following:

Summary

demo_page42
home_page542
Generated Tue Nov 4 10:00:00 1997

After you've been using the hit counter for awhile, take a look at counter.txt that it produces:

demo_page 000000042
home_page 000000542

If you would like to adjust the number of hits recorded, make sure you always have 9 digits for the value. This is necessary because whenever a hit is recorded, rather than rewrite the whole file, the Perl script changes only one line. If you shorten or lengthen a line, then the file can become unusable.

11041997

Author: Doug Steinwand
Date: [11/04/97]
More articles about CGI
More articles by Doug Steinwand
Author Biography