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]


Incrementing the Counter


Incrementing the graphical access counter is similar to incrementing the text access counter. However, the C code for the graphical counter looks dramatically different than the Perl code for the text access counter. Listing 5 contains the C code that reads in the counter value from the counter file, increments the value, and writes the new counter value to the counter file.
Listing 5: The C code for incrementing the counter
/* Counter file handle */
 FILE *counter;

 /* string version of counter file value */
   char s_count[2Ø];

 /* Counter file
     All users need to change the path to the actual
     path for their machines. Windows users need to
     use a path in the form "c:\\robertm\\count.dat" */
 char *cnt_file = "/users/robertm/count.dat";

   /* integer version of count */
 int count;

 /* open the counter file */
 counter = fopen(cnt_file, "r");

 /* check whether the file was opened successfully */
 if (!counter) {
   printf("Content-type: text/html\n\nCannot open counter file!\n");
   return(Ø);
 } else {
   /* if the file was opened, read in the string containing the count */
   fgets(s_count, 2Ø, counter);
   fclose(counter);

   /* extract the integer value of the count from the string */
   sscanf(s_count, "%d", &count);

   /* increment the count */
   count++;

   /* open the count file for output */
   counter = fopen(cnt_file, "w");

   /* check whether the file was opened successfully */
   if (!counter) {
     printf("Content-type: text/html\n\nCannot open counter file!\n");
     return(Ø);
   } else {
     /* if the file was opened, write the count value to it */
     fprintf(counter, "%d", count);
     fclose(counter);
   }
 }





The first several lines define the variables that will be used in this code. After the variable declarations, the counter file is opened with the line
 counter = fopen(cnt_file, "r");


This line opens an input stream, named counter, to the file whose name and path are in the cnt_file variable. The "r" string designates that the file will be opened for input only. It is always a good idea to verify whether the file has been successfully opened. The line
 if (!counter) {


does this by making sure that the counter holds a nonzero value. If the file cannot be opened, the counter variable is set to 0. If counter is equal to 0, the first part of the if...else statement executes. In this section, you output an error message that the file could not be opened.

Under the else portion of the if...else, the access counter value is read in from the file with the line

 fgets(s_count, 2Ø, counter);


This statement reads up to 20 characters from the file pointed to by the counter variable and places the characters in the s_count string. (Actually, s_count is an array of characters, which is the C equivalent of a string.)

After the access counter value is read from the file, the stream to the file is closed. The next line

 sscanf(s_count, "%d", &count);


converts the counter value from a string to an integer and stores the integer value in the count variable, which can then be incremented with the
 count++;


statement.

Finally, the counter file is once more opened, and the incremented value of the access count is written to the file. Notice how the open statement

  counter = fopen(cnt_file, "w");


has the string "w" rather than the string "r". The "w" means to open the file for output. The value of the access counter is written to the file with the line
 fprintf(counter, "%d", count);


which prints the decimal value of the count variable into the file pointed to by the counter variable, which is the access counter file.


[Previous] [Next]