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
The Graphical Counter Script
In the previous two sections, you developed all of the code for the graphical counter script. Listing 8 puts all of the code together and places the necessary #include statements at the beginning. You need to make the specified changes to the variable assignments that assign the path and file names of the counter and digit image files. Because the graphical counter script is written in C, you also need to compile the code before you run it. The procedure for doing so varies depending on what system you are on and which C compiler you have. Using your C compiler, compile the graphical-counter.c file and create the graphical-counter executable file. (If your system restricts you to an eight-character file name, call the file gph-cnt.c and the executable gph-cnt.)
#include <stdio.h>
#include <string.h>
#include "gd.h"
int main(void)
{
/* Counter file handle and image file handle */
FILE *counter;
FILE *image;
/* Input and output images */
gdImagePtr im_file, im_final;
/* Counter file and image files
All users need to change the paths to the actual
paths for their machines. Windows users need to
use paths in the form "c:\\robertm\\count.dat" */
char *cnt_file = "/users/robertm/count.dat";
char *zero_gif = "/users/robertm/Øtiny.gif";
char *one_gif = "/users/robertm/1tiny.gif";
char *two_gif = "/users/robertm/2tiny.gif";
char *three_gif = "/users/robertm/3tiny.gif";
char *four_gif = "/users/robertm/4tiny.gif";
char *five_gif = "/users/robertm/5tiny.gif";
char *six_gif = "/users/robertm/6tiny.gif";
char *seven_gif = "/users/robertm/7tiny.gif";
char *eight_gif = "/users/robertm/8tiny.gif";
char *nine_gif = "/users/robertm/9tiny.gif";
/* string version of counter file value */
char s_count[2Ø];
/* integer version of count, width of image,
x position for new image, and loop variable */
int count;
int width;
int destx = Ø;
int i;
/* the real_height is the height (in pixels) for all of the image files.
the real_width is the width (in pixels) for all of the image files.
Note: If you want to use different digit image files, replace these
numbers with the width and height (in pixels) of the digit image
files you will be using. */
int real_height = 13;
int real_width = 9;
/* 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);
}
}
width = strlen(s_count) * real_width;
im_final = gdImageCreate(width, real_height);
for (i=Ø; i < strlen(s_count); i++) {
switch (s_count[i]) {
case 'Ø':
image = fopen(zero_gif, "rb");
break;
case '1':
image = fopen(one_gif, "rb");
break;
case '2':
image = fopen(two_gif, "rb");
break;
case '3':
image = fopen(three_gif, "rb");
break;
case '4':
image = fopen(four_gif, "rb");
break;
case '5':
image = fopen(five_gif, "rb");
break;
case '6':
image = fopen(six_gif, "rb");
break;
case '7':
image = fopen(seven_gif, "rb");
break;
case '8':
image = fopen(eight_gif, "rb");
break;
case '9':
image = fopen(nine_gif, "rb");
break;
default :
break;
}
if (!image) {
printf("Content-type: text/html\n\nCannot open image file!\n");
return(Ø);
} else {
im_file = gdImageCreateFromGif(image);
fclose(image);
gdImageCopy(im_final, im_file,
destx, Ø, Ø, Ø, real_width, real_height);
destx += real_width;
gdImageDestroy(im_file);
}
}
gdImageInterlace(im_final, 1);
printf("Content-type: image/gif\n\n");
gdImageGif(im_final, stdout);
gdImageDestroy(im_final);
}
|