Adding an Access Counter
A text access counter
The Increment subroutine
The complete text access counter script
Seeding the Counter
Adding the Counter to Your HTML Page
With Server Side Includes
Without Server Side Includes
Creating the Template File
Modifying the access.pl Script
A graphical access counter
Incrementing the Counter
Creating the GIF Image
Returning the Graphical Counter Image
The Graphical Counter Script
Calling the Counter with the <IMG> Tag
Adding an Access Counter
CGI programs can make your Web pages dynamic, and one popular application of CGI is the addition of an access counter. An access counter is a text or graphical representation of the number of times that your Web page has been requested. Access counters work either by including a CGI script within an HTML page with Server Side Includes or the <IMG> tag, or by generating the entire HTML page from a CGI script. When a user requests the HTML page containing an access counter, the CGI script checks the current access count, increments it by one, and displays the results in the Web page. Many Web sites have added access counters to their home pages. Figure 1 displays some ways that access counters can be displayed.

Figure 1: Acccess counters
As shown in the figure, access counters can be displayed as text or as an image file representing the value of the counter. As you might imagine, the graphical version requires more work in the CGI script than simply displaying the value of the counter as text. To display the graphic, you must either have a graphic file for every possible number the counter can reach or you must construct the graphical image from several image files. If you use the latter method, you only need to store 10 files, one for each digit 0 through 9. Then, when necessary, your CGI script creates the image file by putting together the separate GIF graphic files into a single GIF that the browser displays. For example, when the access counter is at 986, the CGI script takes the individual GIF files of the digits 9, 8, and 6 and combines them into one GIF that is the combination of all three. GIF image files are just one of many image file formats. Two other types of image file types that you can work with are JPEG and XBM images, because many Web browsers can display them. However, this article uses GIF image files, as you'll see in the section "A Graphical Access Counter" later in this article.
This article starts with an example text access counter. You should read this section even if you want to create a graphical access counter, because the basic functionality of the two types of counters is the same. The second half of the article explains how to create a graphical access counter. In this case, the process is a bit more challenging, but the results are a lot more impressive.