next up previous contents
Next: Internet Resources Up: Using Perl with Web Previous: Existing Log File Analyzing

Creating Your Own CGI Log File

It is generally a good idea to keep track of who executes your CGI scripts. You've already been introduced to the environment variables that are available within your CGI script. Using the information provided by those environment variables, you can create your own log file.

The cgilog.pl code does ths as follows:

The code for cgilog.pl is:

#!/usr/bin/perl -w


sub writeCgiEntry {
    my($logFile) = "cgi.log";
    my($script)  = __FILE__;
    my($name)    = $ENV{'REMOTE_HOST'};
    my($addr)    = $ENV{'REMOTE_ADDR'};
    my($browser) = $ENV{'HTTP_USER_AGENT'};
    my($time)    = time;


    open(LOGFILE,">>$logFile") or die("Can't open cgi log file.\n");
    print LOGFILE ("$script!$name!$addr!$browser!$time\n");
    close(LOGFILE);
}


writeCgiEntry();


# do some CGI activity here.


print "Content-type: text/html\n\n";
print "<HTML>";
print "<TITLE>CGI Test</TITLE>";
print "<BODY><H1>Testing!</H1></BODY>";
print "</HTML>";

Every time this script is called, an entry will be made in the CGI log file. If you place a call to the writeCgiEntry() function in all of your CGI scripts, after a while you will be able perform some statistical analysis on who uses your CGI scripts.


next up previous contents
Next: Internet Resources Up: Using Perl with Web Previous: Existing Log File Analyzing
dave@cs.cf.ac.uk