#!/usr/bin/perl
# countlog.cgi -- logs Web page hits and passes control to GIF counter
# the first line of this script may have to be changed
# if your system's Perl interpreter is not located in /usr/bin/perl
# Written by P. Lutus Ashland, Oregon lutusp@arachnoid.com 6/2/96
# This script logs accesses but requires a CGI counter program written by others
# Call the counter like this: You are Visitor Number
# Ownername can have particular web page designators attached to it like this:
# Ownername_mypagename
# This allows you to count pages independently.
# The directory in which countlog.cgi is stored must have world read, write
# and execute access. The file countlog.cgi must have world execute access.
# And believe me when I tell you -- this is just the start of the requirements for
# a successful page counter.
# customization area -- begin
# set $collect_stats = 1 if you want to log the origins of accesses
# in a file named Ownername.log
$collect_stats = 1;
$countname = "counter_dat"; # default counter file name
# $countpath is the path to the counter files used
# by the CGI counter program. Usually not in cgi-bin, so there
# has to be a relative path
$countpath = "../counter/data";
# $countvar is the variable name used by the CGI counter program
# for the counter file
$countvar = "df";
# customization area -- end
@envname = (
'REMOTE_ADDR',
'REMOTE_HOST',
'HTTP_REFERER',
'HTTP_USER_AGENT',
'HTTP_ACCEPT',
);
if ($ENV{COMSPEC} ne '') { # most definitely MSDOS, so use those old funky names
@envname = keys(%ENV);
}
if ($ENV{'QUERY_STRING'} ne '') {
$buffer = $ENV{'QUERY_STRING'}; # get the file name from counter.cgi?ownername construct
@pairs = split(/&/,$buffer);
foreach $pair (@pairs) {
($name,$value) = split(/=/,$pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$FORM{$name} = $value;
}
$countname = $FORM{$countvar}; # get the name of the counter file
}
# now test to see if this is a valid acess
$qsn = $ENV{'SERVER_NAME'};
$qsn =~ s/www.//ig; # remove possible server-name prefix
$qsn =~ s/.com//ig; # remove server-name suffix
if ($ENV{'HTTP_REFERER'} =~ /$qsn/) { # log only if the root server name appears in the referer path
unless (-e "$countpath/$countname") { # unless this file already exists
open (COUNT,">$countpath/$countname"); # directory has to have world write permission for this to work
print COUNT "0\n";
close COUNT;
}
$count = 0; # now get and increment the counter to stay synced with the CGI program
open (COUNT,"$countpath/$countname") || die "Content-type:text/plain\n\nCan't open $countname!\n";
$count = ;
close COUNT;
$count++; # so the log count agrees with GIF counter
if ($collect_stats) { # owner want to collect data on logons
$logname = $countname . ".log";
unless (-e $logname) { # unless this file already exists
open (LOG,">$logname"); # directory has to have world write permission for this to work
print LOG "Count\tTime";
foreach $envlbl (@envname) {
print LOG "\t$envlbl";
}
print LOG "\n";
close LOG;
} # done initializing stats file
open (LOG,">>$logname"); # now enter this stat record
$tim = &readtime;
print LOG "$count\t$tim";
foreach $envlbl (@envname) {
print LOG "\t$ENV{$envlbl}";
}
print LOG "\n";
close LOG;
} # end of collect_stats block
# pass control to CGI counter with all the same variables
print"location:Count.cgi\?$buffer\n\n";
} # validity test
exit;
sub readtime {
local ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
sprintf ("%2.0f/%02.0f/%04.0f %2.0f:%02.0f:%02.0f",$mon+1,$mday,($year > 50)?$year+1900:$year+2000,$hour,$min,$sec); # to the stack
}