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

[Previous] [Next]


Modifying the access.pl Script


After you have created the template file, you must modify your access.pl script to open the template file, read in the contents, change the XXXX placeholder into the current access count, and output the HTML code to the Web browser. It is easiest to add this code in a subroutine called Display_Page and simply call the subroutine after the counter has been incremented. Listing 3 contains the Perl code for this subroutine.
Listing 3: The Display_Page Subroutine
sub Display_Page {
   local ($count) = @_;

   # All users need to change the following path
   # to be correct for their system. Windows users
   # need it in the form "c:\\robertm\\text.tmpl".
   local ($html) = "/users/robertm/text.tmpl";
   local (@template);

   open(TEMPLATE, "$html") || die "Content-type: text/html\n\nCannot
   open template!";
   @template = <TEMPLATE>;
   close(TEMPLATE);

   # Be sure to change the index number to the correct one
   # for your HTML template. Count down from the first line
   # being at Ø to the line containing the XXXX placeholder.
   $template[7] =~ s/XXXX/$count/e;

   print "Content-type: text/html\n\n";
   print @template;

 }





The Display_Page subroutine begins by declaring the two variables and one array that will be local to this subroutine. The variable $html stores the name and path to the template file that the access counter will be added to. Then the template file is opened and all of its contents are read into the @template array. In this example, the placeholder for the access number XXXX is on the eighth line of text.tmpl file. When the file is read into the array, the eighth line is placed in the eighth element of the @template array. Because the index of Perl arrays begins with 0, the eighth element is at index 7. So, the statement
 $template[7] =~ s/XXXX/$count/e;


takes the eighth element of the @template array and replaces the access number placeholder with the current count. If you use this subroutine for your HTML template, you need to change the index of the @template array element to the correct index for your template file.

After the placeholder for the access count is changed to the actual count number, the required parsed header and the contents of the @template array are returned to the user's Web browser. Because this entire page is being generated from the CGI script, you need to change all the links to this page to call your CGI script, access.pl, which should now be modified to contain the new Display_Page subroutine. Listing 4 is the Perl code for the new access.pl script. Notice how the two lines that previously output the parsed header and the access count have been replaced with a call to the Display_Page subroutine. Don't forget to modify the paths for the $file and $html variables to contain the correct paths for your system. Also, Windows users must remember to remove the first line of the script file. Figure 3 shows how Netscape displays a call to the modified access.pl script when the counter value is 13. Notice that the page looks the same as the one in Figure 2, which is called with a Server Side Include.

Listing 4: The modified access.pl script
#!/usr/local/bin/perl

 # All users need to change the value of this
 # variable to the path for their machine. Windows
 # users need to use a format similar to
 # "c:\\robertm\\count.dat"
 $file = "/users/robertm/count.dat";

 $access_number = &Increment;
 &Display_Page($access_number);

 sub Increment {
   local ($count);

   # Get the current value of the access counter.
   open(COUNT, "$file") || die "Content-type: text/html\n\nCannot open
   counter file!";
   $count = <COUNT>;
   close(COUNT);

   # Increment the access counter

   $count++;

   # Store the value of the counter in the counter file.
   open(COUNT, ">$file") || die "Content-type: text/html\n\nCannot open
   counter file!";
   print COUNT $count;
   close(COUNT);

   return $count;
 }

 sub Display_Page {
   local ($count) = @_;

   # All users need to change the following path
   # to be correct for their system. Windows users
   # need it in the form "c:\\robertm\\text.tmpl".
   local ($html) = "/users/robertm/text.tmpl";
   local (@template);

   open(TEMPLATE, "$html") || die "Content-type: text/html\n\nCannot
   open template!";
   @template = <TEMPLATE>;
   close(TEMPLATE);

   # Be sure to change the index number to the correct one
   # for your HTML template. Count down from the first line
   # being at Ø to the line containing the XXXX placeholder.
   $template[7] =~ s/XXXX/$count/e;

   print "Content-type: text/html\n\n";
   print @template;

 }






Figure 3: The results of the modified access.pl


[Previous] [Next]