Jon's WWW server
include access counter


This counter relies on server side includes to execute the counter script from within a server parsed html document. When the script is executed, it gets the name of the page that called it from the environment, assembles the full path to the page, opens the corresponding counter file, increments the counter, displays the current count and saves the count back to the counter file. When opens a .count file it locks the file to prevent other instances of or other programs from messing with the count file while it's in use. If your pages see heavy traffic, it's important to use file locking to keep your counters from being corrupted by multiple simultaneous accesses. When a second instance of tries to open and lock an already locked .count file, the second instance will wait for the first lock to be released before continuing. At least that's what happens with perl 5 under Linux.
This counter has certain advantages and disadvantages compared to graphical access counters. Advantages are, that the counter counts all client types, not just those that can load images, and that there's a little less bandwidth used in sending a few characters than in sending a bitmap. Disadvantages are that it requires server side includes which is said to possibly slow down the httpd and can be a security problem in some cases.


The script only outputs a number, so any text you want around the number has to be coded into the html document. This is how I usually call the script:

There have been <font size="+1"> <!--#include virtual="/cgi-bin/" --></font> accesses to this page since May 25, 1995

The Netscape specific font size change is of course totally optional.


To set things up, simply download the script and perl include. Install these in a CGI executable directory and make executable. Then create file.count files for all the html pages you plan to add access counters to. i.e. If you wanted to add a counter to the file index.html, you would create the file index.count in the same directory as index.html. You can either use an editor to do this or just touch the file. A zero length .count file is the same as a .count file with 0 in it. If you're converting over from another counter system, you can stuff the counter by creating the .count file with an editor and placing the current count on line one of the file. The .count files must be readable and writable by the uid that runs under or will print an error message instead of a number. The simplest way to guarantee the server has write access to to chmod 666 file.count. This, of course, means that anyone else on your system could reset or trash your counter files, but hopefully they won't. If you can chown your .count files to the uid that will run under, then you could use 664 or 644. Many systems will not allow anyone other than root to chown files though, and other users could still write CGI scripts to trash your counters.

Revision History: First version publicly made available. Same as c2, except instead of hardcoding the user home dir, getpwnam is used to find the user's home dir if the page is a user page. This makes c3 much more portable...assuming getpwnam is supported on the target platform. Don't hardcode htroot virtual servers can use a single script...only known to work with Apache. Same as c3, except the extension .html is no longer hard coded. c4 will determine the extension of the calling html file and swap .count for c4 can be used from .html, .htm, .whatever files. Slightly more informative error reporting.


If you're interested in a graphical WWW page counter, I strongly recommend checking out Eugene E. Devereaux's number.xbm perl counter. I used his counter for quite some time before writing my own. In some ways it's more complex, in others it's not. Rather than printing out text for the counter, it spits out an xbm format image. It's a bit more foolproof in that you pass it the entire path to the count file, so it doesn't have to figure out what the count filename is. One disadvantage is that text based browsers won't "hit" it and increase the counter. Another is that I just don't like the way number.xbm handles accessing/locking the counter file...but that's just opinion and easy enough to change. Gene's counter can be gotten from

How'd he do the white background?:

This doesn't really belong here...but I didn't want to create a new page for it.... Those of you who like to experiment with netscape extensions may or may not know that you can now set the background, text, and link colors to any color you like. Netscape was evil though, and chose to implement color values as hex triplets rather than the decimal numbers most humans are brought up on. To make the conversion of decimal triplets such as 128 128 128 to hex triplets like 808080, I wrote a simple little perl program. The full source...all couple available here.

There have been 33967 accesses to this page since May 25, 1995

This page was last modified Friday, 09-Apr-1999 14:52:53 EDT

Comments to: Jon Lewis