A Personal Hit Counter ()

This hit counter is different in that it attempts to record the number of genuine visitors to your web pages. Although the installation requires more effort than with commercial counters, you can be confident in knowing exactly what you are counting, and that you have a flexible counter which is under your control.

Providing the user allows session cookies and JavaScript is enabled, a hit is recorded once per browser session. Hits from all common robot software agents are excluded, and additional excluded robots may be easily added to the list (see Andreas Staeding's site for a comprehensive database of software agents).

If the user disallows session cookies, or JavaScript is not enabled, a hit is recorded once per counter access.

Multiple counters are supported. A counter may be shared across web pages, or limited to one web page; you may have more than one counter on a page; and an unlimited number of web counters may be associated with a web site. A cumulative count is maintained for each counter, and monthly totals are also recorded.

The current count of hits may be optionally displayed on the page, as above.

The main counting software consists of a Perl script, hitcounter.cgi within the server; and a server-side include line and a line of JavaScript within the web page. Because of the server side include, those web pages which include a counter, should have an extension of "shtml".

The Perl script should be held in the server's appropriate cgi bin, and given execute privileges (on a *nix system, this will require a CHMOD to #755). Counters are held in small text files, one per counter, stored by default in the "data" subdirectory within the cgi bin directory. There is a line in the Perl script that allows this relative address to be changed.

my $directory = "data/";

The files are created automatically, but once created, the files may be manually edited to initialise the counts to the required value.

Within the Perl script, there is a list of user agents from whom hits are ignored, such as search robots and link checkers. All the common ones are included, but if you do need to add another agent, they are specified as sets of case insensitive strings separated by the pipe character. In the following example, we are adding three entries to the list held in the string robotlist.

$robotlist = $robotlist . "|googlebot|xenu|scooter";

No other changes need to be made to the Perl script, although you can also change the hit counter message that is displayed on the bottom line:

my $message = "You are visitor no:";

The following two lines need to be inserted into the web page - typically where the hit counter would be displayed.

<!--#include virtual="/cgi-bin/hitcounter.cgi?counter=countername&display=true"--> 
<script type="text/javascript">document.cookie = "countername=0";</script>
  1. /cgi-bin/ needs to be the absolute directory where the Perl script may be found.
  2. counter=countername determines the name of the counter to update.
  3. display=true determines whether the counter is to be displayed. A value of false indicates that it shouldn't.
  4. countername=0 is the name of the cookie to be set. This must be the same as the name of the counter set in the line above. The value =0 should stay the same.

Note that if your counter is accessed from pages stretched across directories, you will need to set the path attribute of your cookie. Thus the following cookie will be shared by all pages within and below the chartreuse directory.

<script type="text/javascript">
  document.cookie = "countername=0 path=/chartreuse";
</script>

You may view the total hits notched up for the counters from the hitstats page. This uses a Perl script to generate and display the current state of the hit counters. Selecting one of the individual counters will bring up a further display of all monthly totals recorded for that counter using the web page hitstats1. Both of these scripts should be stored in the same directory.

The Perl script associated with these displays, hitstats.cgi and hitstats1.cgi, should be held in the server's appropriate cgi bin, and given execute privileges as above. There is a line in the Perl script that allows the subdirectory where the counter files are held to be specified:

 my $subdir = "data/";

The HTML files contains a line specifying the Perl script. This should be modified to reflect the actual location of the script as above:

<!--#exec cgi="/cgi-bin/webstats.cgi"-->

The Perl scripts were developed and tested on an Apache server. The author would be grateful for news of successful or otherwise attempts to install the software on other servers.

Download Zip File (12 Kb).