README. Ian's Log Script By Ian Porteous As amended by Chris Davis and Chris's HTML log output By Chris Davis Visit the CGI Server FAQ at www.thornlea.force9.co.uk Notes... See the comments at the start of the .PL files. Make sure that you upload the access, counter, statistics and log.pl as ASCII and dummy.gif as BINARY. Don't forget to alter the paths to reflect your directory And make the access and counter files readable and writable... (chmod 666) And in the page calling the log script insert

towards the end. And the page calling the statistics script (can be called from the same page) insert

CLICK HERE TO VIEW THE LOG FOR THE SCRIPTS PAGE

******************************************************************************************************* LOG.PL #!/usr/bin/perl ############################## # Ian's Counter / Log script. # ############################## # #################################################################### # Amended by Chris Davis to:- # # change field separator to a ',' thus emulating a *.csv file, # # use 'gethostbyaddr' to try to obtain a little more information # # amending date format to dd/mm/yy rather than mm/dd/yy # # incrementing month value by one to ensure e.g. April recorded as 4 rather than 3 # #################################################################### # # Uses: counts the number of visits to your site. It's called from an IMG tag in ANY HTML # page - so you can use it from any website. # If you make more than one copy of this script - you can place it in different directories # and use them to monitor different sites. # # It also produces a log file which stores the browser type, date, remote host and refferer # so you can emulate the logs produced by the webserver - (which we don't usually get) # We expect you could use a tool like analog to analyse these files, but Ian will be creating a # script to do that soon. # # This script requires 3 extra files. For neatness we use the same directory - in this case withing cgi-bin and called logs # # 1. A .gif file called dummy.gif - please try to keep it small or it'll # slow down the server. # # 2. Counter - this is a text file used to store the hit count - to create it # either upload a blank text file called conter using ftp or from a telnet # session make sure you're in the dir with the scripts and type # # echo "" > counter # # # 3. Access - another text file - this is used to store the logs of visitors # once again, upload a blank one - or do the following # # echo "" > access # # The access, count AND the script files MUST be uploaded as ASCII files. # The gif should be uploaded as binary # # Make sure that you give rw (666) access to the text files # and rwxr-xr-x (755) to the scripts # and ensure the logs directory is 777 rwxrwxrwx # Change this to 0 if you only wish to count visitors $uselog = "1"; # replace "username" with your username $count_file = "/files/home/username/cgi-bin/logs/counter"; $access_log = "/files/home/username/cgi-bin/logs/access"; # opens the image file - reads it and closes it open( IMG, "/files/home/username/cgi-bin/logs/dummy.gif"); $/ = undef; my $img = ; close IMG; # do all the complicated stuff # note next to last line incementing date value by one as January = 0 if ($uselog == 1) { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); if ($sec < 10) { $sec = "0$sec"; } if ($min < 10) { $min = "0$min"; } if ($hour < 10) { $hour = "0$hour"; } if ($mday < 10) { $mday = "0$mday"; } if ($mon < 10) { $monc = "0$mon"; } $mon = $mon+1; $date = "$hour\:$min\:$sec $mday/$mon/$year"; } open(COUNT,"$count_file") || die "Can't Open Count Data File: $!\n"; $count = ; close(COUNT); if ($count =~ /\n$/) { chop($count); } # the following routine uses gethostbyaddr to try and get a little more information about the user $packed_address = pack("C4",split(/\./,$ENV{"REMOTE_ADDR"})); ($host,$aliases,$type,$length,@addrs) = gethostbyaddr($packed_address,2); $addresses=""; foreach(@addrs) { $addresses .= join(".",unpack("C4",$_)) . " "; } $count++; open(COUNT,">$count_file") || die "Can't Open Count Data File For Writing: $!\n"; print COUNT "$count"; close(COUNT); open(LOG,">>$access_log") || die "Can't Open User Access Log: $!\n"; print LOG "$date,$ENV{'HTTP_REFERER'},$ENV{'REMOTE_ADDR'},$ENV{'REMOTE_USER'},$ENV{'REMOTE_HOST'},$ENV{'HTTP_USER_AGENT'},$host\n "; close(LOG); print "Content-type: image/gif\n\n"; print $img; exit(0); ****************************************************************************************************** STATISTICS.PL #!/usr/bin/perl ############################################# # Chris's HTML counter and access log viewer# ############################################# # # replace "username" with your username $access_log = "/files/home/username/cgi-bin/logs/access"; $count_log = "/files/home/username/cgi-bin/logs/counter"; # opens count file open (FILE, "$count_log"); @lines = ; close (FILE); # Print out the Content-type headers and the heading text # Change Title and Headers to meet your own requirements and tastes. print "Content-type: text/html\n\n"; print <<"EndOfHTML"; "Enter1819 Log"

Log Entries for scripts.htm

Environment and User Information


EndOfHTML # Prints out the number of accesses print ("The Scripts Page has been visited @lines times since 5/4/1999"); # opens access file open (FILE, "$access_log"); @lines = ; close (FILE); # Now identify each row and each field (in this case "," as field separator foreach (@lines) { chop; ($date, $referer, $addr, $user, $host, $agent, $hostname) = (split(/,/)); $date = "" if !defined($date); $referer = "" if !defined($referer); $addr = "" if !defined($addr); $user = "" if !defined($user); $host = "" if !defined($host); $agent = "" if !defined($agent); $hostname = "" if !defined($hostname); # Note in above use of "if" to allow empty fields # Now specify start of HTML to print each record in turn print <<"EndOfHTML";
EndOfHTML print(" Time/date=$date, Referer=$referer, Address=$addr, User=$user, Host=$host, Agent=$agent, HostName=$hostname \n"); print <<"EndOfHTML";
EndOfHTML } exit(0);