Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl # This script saves access date+time, the visitor's host name, their I +P # address and browser type, and the refering page (if any) to a log fi +le # on your server (default name = logfile.txt). This file will be autom +atically # mailed to your mailbox. You can change the mailing interval accordin +g # to your needs (default value is every 50 accesses to the page to be # logged). The script will autocreate the logfile, if it does not exis +t. # don't change anything past this line unless you know what you are do +ing # -------------------------------------------------------------------- +--- # create a date+time string $shortdate = `date +"%D %T %Z"`; chop ($shortdate); # Some of Perl's network info functions required here ($part1,$part2,$part3,$part4)=split(/\./,$ENV{REMOTE_ADDR}); $IP_adr=pack("C4",$part1,$part2,$part3,$part4); ($host_name)=(gethostbyaddr("$IP_adr", 2)); #print "Content-type: text/plain\n\n"; if ($check_host) { # read host and time info from last visitor if (-e "$hostfile") { open(HOST,"$hostfile"); $hostline = <HOST>; chop($hostline) if $hostline =~ /\n$/; close(HOST); ($old_time,$old_number,$old_page,$old_browser) = split(/\|/,$hos +tline); } # save host and time info and check if this is a page reload open(HOST,">$hostfile"); $seconds = time; print HOST "$seconds\|$ENV{REMOTE_ADDR}\|$ENV{'DOCUMENT_URI'}\|$ENV +{'HTTP_USER_AGENT'}"; close(HOST); if (time - $old_time < $interval && $ENV{REMOTE_ADDR} eq $old_number && $ENV{'DOCUMENT_URI'} eq $old_page && $ENV{'HTTP_USER_AGENT'} eq $old_browser) { exit; # probably same visitor, so exit } } # open log file for output and append new log data open (LOGFILE, ">>$logfile"); print LOGFILE "Time: $shortdate\n"; print LOGFILE "From: $ENV{'HTTP_REFERER'}\n"; print LOGFILE "IP : $ENV{REMOTE_ADDR}\n" if $log_IP; print LOGFILE "Host: $host_name\n"; print LOGFILE "With: $ENV{'HTTP_USER_AGENT'}\n"; print LOGFILE "Page: $ENV{'DOCUMENT_URI'}\n" if $log_page; print LOGFILE "\n"; close (LOGFILE); # open log file for input and count log entries open (LOGFILE, $logfile); @entries = <LOGFILE>; close (LOGFILE); $log_rows = 7; $log_rows-- unless $log_page; $log_rows-- unless $log_IP; $log_count = @entries/$log_rows; # if number of logs >= max. number of logs, mail file and delete it if ($log_count >= $max_entries) { open (MAIL, "|$mailprogam $recipient") || die "Can't open $mailprog +am!\n"; print MAIL "Subject: Mail-log File\n\n"; print MAIL "@entries\n"; close MAIL; unlink $logfile; } # end of script
Janitored by Arunbear - added readmore tags, as per Monastery guidelines
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Making script called from image tags
by jhourcle (Prior) on May 05, 2005 at 13:29 UTC | |
by ww (Archbishop) on May 05, 2005 at 14:39 UTC | |
by jhourcle (Prior) on May 06, 2005 at 02:22 UTC | |
| |
by Anonymous Monk on May 05, 2005 at 14:18 UTC | |
|
Re: Making script called from image tags
by Joost (Canon) on May 05, 2005 at 11:41 UTC | |
| |
|
Re: Making script called from image tags
by Fletch (Bishop) on May 05, 2005 at 17:53 UTC | |
|
Re: Making script called from image tags
by starbolin (Hermit) on May 05, 2005 at 17:53 UTC | |
by Anonymous Monk on May 06, 2005 at 11:31 UTC | |
by starbolin (Hermit) on May 06, 2005 at 18:14 UTC | |
by Anonymous Monk on May 06, 2005 at 19:05 UTC |