#!/usr/bin/perl use strict; #------------------------------------------------------------------# # # Countalog v0.10 # # This script counts pageviews for a single page. It can be set to # store just the number of pageviews or it can be set to write a log # with information about each visit. It also accepts a list of IP # addresses and host names that should not be counted. Further # information is available at www.faketp.com. # #------------------------------------------------------------------# # # By default, the script will both count pageviews and log them. You # can change the default by specifying a "yes" value for $count_only. # If you want to use the defaults, give an empty value ("") to this # variable. my $count_only=""; # By default, the script looks for a directory named 'countalog' to # be located below it. For example, if you are counting visits to # your home page at http://you.com/index.shtml, the script will try # writing to http://you.com/countalog/index.html and # http://you.com/countalog/count.txt. You can change the defaults # by specifying values for $log_to and $count_to. If you want to use # the defaults, give an empty value ("") to these variables. my $count_to=""; my $log_to=""; # You can specify IP addresses and domains to be excluded from your # counting and logs using the @ignore variable. By default, the # script ignores IP address 127.0.0.1, any IP address beginning with # 10.10.40., and it ignores the host dsl.ignore.com and anything in # the nolog.net domain. You customize the exclusion list here: my @ignore=qw( 127.0.0.1 dsl.ignore.com 10.10.40. nolog.net ); #------------------------------------------------------------------# #--------- NO CONFIGURATION REQUIRED BELOW THIS POINT -------------# #------------------------------------------------------------------# my ($theDate, $no, $b, $ref, $ip, $host, $skip); visitorinfo(); for (@ignore) { if ($ip =~/$_/ || $host =~/$_/i) { $skip=1;} } doit() unless ($skip) ; #-----------------# #--- FUNCTIONS ---# #-----------------# sub visitorinfo { $b = $ENV{'HTTP_USER_AGENT'}; $ref = $ENV{HTTP_REFERER}; $ip = $ENV{REMOTE_ADDR}; $host = $ENV{REMOTE_HOST}; return $b, $ref, $ip, $host; } sub doit { theDate(); number_inc("$count_to"); logit("$log_to") unless ($count_only); sub theDate { open (DATE,"-|") || exec 'date +"%m/%d/%y at %H:%M"'; $theDate=;close DATE; return "$theDate"; } sub number_inc { my $count = shift || "countalog/number.txt"; open (NUMBER, "<$count"); $no = || &set_number; sub set_number { open (NUM, ">$count"); print NUM "0";close (NUM); $no = "0"; } close (NUMBER); $no++; open (NEW, ">$count") || die("Can't open for writing the number file $count."); print NEW "$no"; close (NEW) || die("Can't close the number file $count."); return $no; } sub logit { my $logfile = shift || "countalog/index.html"; open (LOG, ">>$logfile") || die("Can't open logfile $logfile."); flock (LOG, 2) || die("Can't lock the logfile $logfile."); print LOG "$no$ref, $b$theDate$ip$host\n"; flock (LOG, 8) || die("Can't unlock the logfile $logfile."); close (LOG); } }