Perl Script
No comment
Download Count: 134
Date Added: Sunday, 03-Jul-11 21:42:51 CDT
Tags: perl, cgi, hit counter
A simple perl hit counter and logger, no bells or whistles, just counts every page hit, and does some simple logging. Also adds the proper ending to the displayed count, i.e. 1st, 2nd, 11th, etc.
Make sure the file is executable (chmod 755 file_backup.sh) and include it in any web page with the following code snippet: <!--#exec cgi="http://path to file/log_counter.pl"-->.
Depending on your web server settings, you may need to name your web page files with the extension of .shtml instead of .html.
File Name: log_counter.pl - Code Type: Perl
#!c:\perl\bin\perl.exe ### ERROR LOGGING ## use CGI::Carp qw(fatalsToBrowser); BEGIN { use CGI::Carp qw(carpout); my $error_log = "./counter_error_log.txt"; open(LOG, ">$error_log") or die("Unable to open $error_log: $!\n"); print LOG "Errors:\n"; carpout(\*LOG); } ### END ERROR LOGGING ## $counter_file = "counter.txt"; $log_file = "log.txt"; $gmtPlusMinus = 0; #real local time i.e. gmt time zone local(($sec,$min,$hour,$mday,$mon,$year)) = (gmtime); $mon = $mon+1; $year = $year+1900; $today = sprintf("%04d%02d%02d%02d%02d%02d",$year,$mon,$mday,$hour,$min,$sec); open (COUNT, "$counter_file"); $counter = <COUNT>; close (COUNT); open (COUNT, "> $counter_file"); $counter++; print COUNT "$counter"; close (COUNT); open (LOG, ">>$log_file"); print "Content-type: text/plain\n\n "; print LOG "$today|$ENV{'HTTP_USER_AGENT'}|$ENV{'HTTP_REFERER'}"; print LOG "|$ENV{'REMOTE_HOST'}|$ENV{'REMOTE_ADDR'}\n"; close (LOG); print "$counter "; &ending; exit; sub ending { @digit = split(//, $counter); if ( $digit[-1] == 0 ){ print " th"; } if ( $digit[-1] == 1 && $digit[-2] == 1){ print " th"; } elsif ( $digit[-1] == 1 ) { print " st"; } if ( $digit[-1] == 2 && $digit[-2] == 1){ print " th"; } elsif ( $digit[-1] == 2 ) { print " nd"; } if ( $digit[-1] == 3 && $digit[-2] == 1){ print " th"; } elsif ( $digit[-1] == 3 ) { print " rd"; } if ( $digit[-1] == 4 ){ print " th"; } if ( $digit[-1] == 5 ){ print " th"; } if ( $digit[-1] == 6 ){ print " th"; } if ( $digit[-1] == 7 ){ print " th"; } if ( $digit[-1] == 8 ){ print " th"; } if ( $digit[-1] == 9 ){ print " th"; } }
Parsed in 0.042 seconds - Rate: 39.32 KB/s - GeSHi version: 1.0.8.10
Hope you find this helpful! If you have any questions, please leave a comment.