Perl hit counter tutorial. In this article learn how to make a simple hit counter for your website using Perl CGI!  

Articles > Guest Articles > Writing a simple hit counter using Perl CGI

Writing a simple hit counter using Perl CGI

| by John Collins

Hit counters are used on web sites to determine the popularity of a page. When a visitor opens (or “hits on”) a page, the hit counter increments by one to account for this hit. In this introductory tutorial on CGI (Common Gateway Interface) and Perl, I will show you how to write a basic Perl hit counter that will carry out this function. Our example will consist of the following files:

1. counter.cgi - the actual Perl hit counter script, to be placed in the cgi-bin of your web site.
2. count.dat - a plain-text file that will store the count value, also in the cgi-bin.
3. counter demo.shtml - the HTML page that will call the script, I will explain the '.shtml' file extension later.

The Perl script

This is the contents of counter.cgi


# the path for the log file
$logpath = "count.dat";

# digits for the counter
$pad = 5;

# opens the log file for reading, will be created if none exists.
open (LOG, "$logpath");
@file = <LOG>; # an array of the file contents

$count = $file[0]; # the count value is the first line in the file, i.e. $file[0]
$count++; # increments the counter value

open (LOG, ">$logpath"); # opens the log file for writing
flock(LOG, 2); # file lock set
print LOG "$count\n"; # prints out the new counter value to the file
flock(LOG, 8); # file lock unset

# sets the leading zeros padding for the counter
$pad = "%.$pad"."d";
$count = sprintf($pad, $count);

# prints out the counter
print "Content-type: text/html\n\n";
print "$count";

The perl hit counter script begins with the all-important shebang line, which may be different for your script, dependant on your server. Then the $logpath scalar variable is used to store the location of the count.dat file, which contains the current count value for the page.

The variable $pad may be changed to suit your needs, it specifies the amount of digits that will be in the count value that is displayed. The count.dat file is opened, and assigned the label 'LOG', then it's contents are read into the @file array (actually, it's contents are only one line, i.e. the count number). If this file does not exist, it will be created automatically, and it is important that this file's file permissions are set so that it can be modified later.

$count = $file[0];

These two lines are the main counting function. The first line says that the $count scalar is the same value as the first value (line) of the @file array, namely $file[0] (array indexing starts at 0, as with most languages). The second line then increments this value; this line is the same as saying "$count = $count + 1;". Now we are ready to save our updated counter value.

Notice how the second 'open' statement includes a '>'? This is to indicate that the file is being opened for writing, the original file contents will be lost. The 'flock(LOG, 2)' statement is very important. This is the file-lock (flock) command, which means that as the file is locked by this process, no other processes can access the file. This ensures that two hits to the page simultaneously will not erase the contents of the file, which is a real possibility without file-locking. The code 2 locks the file, while 8 unlocks it.

Finally, the 'sprinf()' function is used to format our output with zero padding, and the value of $count is outputted using the 'print' command.

Calling the Perl script from the HTML page

This is the contents of counter demo.shtml

<title>Basic Hit Counter Example</title>

<style type="text/css">
p {font-family:"Century Gothic", "Sans Serif"; font-size:12pt; color:black;}

<p>You are person number
<!--#include virtual="/cgi-bin/counter.cgi" -->
to view this demo!</p>

The key line in this file is highlighted in red above. This is a Sever Side Includes (SSI) directive, which means that it will be carried out by the server before the HTML page is sent to the user. In other words, the CGI script will be executed first, then the result (output) of this script will be included in the HTML sent to the user. The '.shtml' file extension is required by most servers for files that contain SSI directives.

All that is left now is to show a demo. View demo

Concluding notes

Perl is not an easy language to learn. In fact, it's quirky syntax can seem somewhat confusing to anyone starting to learn the language. It is not very forgiving, a slight syntax error, such as leaving out a semi-colon at the end of a line of code, will cause your entire script to fail. However, if you stick with it, there are very few limits to what you can achieve with this powerful language. I hope that this simple introductory example will encourage you to seek out more Perl CGI examples online.

About the author

Article by John Collins. John is a freelance web designer from Dublin. He is webmaster of Visit his site for more articles and tutorials on web development and technology related topics.