[an error occurred while processing this directive] CGI - A free tutorial for the Total Non-Programmer.
CGI - Let's get startedCGI - Introduction to Perl

Hit Counters


Up until now, every CGI we have used has required the user to hit a 'submit' button on an HTML page, at which point the entire window is replaced by a new page.  You could easily write a program using the skills you have already learned to open a count file, add 1, and replace.  It would look something like this:

#******** BEGIN BODY ********
open (GETCOUNT,"<count.log");            #open count log for input
$counter=<GETCOUNT>;                      #assign contents of file to $counter
close (GETCOUNT);                              #close access to file

$counter++;                                                 #increase $counter by 1.
print "$counter hits";                                #print number of hits to users screen

open (PUTCOUNT,">count.log");            #open count log for output
print PUTCOUNT ($counter);                #replace old counter value with new one
close PUTCOUNT;                                # close access to file

#******** END BODY *********

If we were to call such a program with a SUBMIT button, the current page would dissapear and be replaced by the output of this program.  In order to display the hits on our page, we need a way to execute a program when a page loads, and embed the results back in that same page.  This method is called a Server Side Include (SSI).

NOTE: Not all web servers support SSI.  Some only work if the pagename ends with  ".shtml" or ".html-ssi" and some have it completely disabled for security purposes.  If you have difficulty with this exercise, check with your system administrator.

Here's how it works:

If nothing shows up, browse to count.cgi to see whether it works, or returns an error.  Once you know the cgi works, try renaming the html page with an .shtml extension (for SSI enabled).

That's all so far, please send me your comments


BACKHTML Form | Guestbook Form | Hit Counters

Home | WebData - Web Database Software | Javascript | CGI | Consulting | Map Builder | Contact Us | The Press Room