UBB.Dev
Hi

I have a script to log and track hits to my website. Here it is :

#!/usr/bin/perl

################### Copyright ##################################################
# This script is copyright by CGI-Center.com 1999. All rights reserved and so #
# on. You are not allowed to resell or destribute this script in any way blah #
# blah blah blah blah. #
################################################################################
# For help, suggestions or correction regarding this script please visit our #
# CGI forum at http://www.cgi-center.cx/forum/ #
################################################################################

################### Variables ##################################################
$password = "guest";
# Your admin password. #
#
$image = "dot.gif";
# The URL of your graph-image. You are welcome to use your own image for this. #
################################################################################
# Define what statistics you want on the admin area. If you do not define them #
# they will still be available by pressing a button on the admin area. #
$last_refers = "yes";
#
$top_refers = "yes";
#
$last_visitors = "yes";
#
$history = "yes";
#
$top_pages = "yes";
#
$search = "yes";
################################################################################
# How big will you allow your log file to grow? Defined in kb. There is no #
# limit to how big it may be but less than 1000 kb is advicable. #
$max_log = "500";
################################################################################

# No more editing is needed below this line
#
#
# # #
# # #
###
#
###############################
&getinput;

print "Content-type: text/htmlnn";

$ip = $ENV{'REMOTE_ADDR'};

if ($INPUT{'password2'}) {&admin;}
elsif ($ENV{'QUERY_STRING'} eq "login") {&login;}
elsif ($INPUT{'login'}) {&admin;}
elsif ($INPUT{'history'}) {&history;}
elsif ($INPUT{'last_visitors'}) {&last_visitors;}
elsif ($INPUT{'last_refers'}) {&last_refers;}
elsif ($INPUT{'top_refers'}) {&top_refers;}
elsif ($INPUT{'search'}) {&search;}
elsif ($INPUT{'searchfor'}) {&searchfor;}
elsif ($INPUT{'searchfor2'}) {&searchfor;}
elsif ($INPUT{'top_pages'}) {&top_pages;}
elsif ($INPUT{'reset'}) {&reset;}
else {&count;}

sub login {
print "
Enter admin password below to login:
n";
print "
";
}

sub count {
&get_date;


unless (-e "date.txt") {
open (DATE,">date.txt");
print DATE "$today";
close (DATE);
}
unless (-e "ips.txt") {
open (IPS,">ips.txt");
close (IPS);
}

open (DATE,"date.txt");
$oldday = ;
close (DATE);

if (($oldday =~ /[0-9]/) && ($oldday < $today)) {
open (DATE,">date.txt");
print DATE "$today";
close (DATE);

open (COUNT,"count.txt");
$count = ;
close (COUNT);
chop ($count) if ($count =~ /n$/);

open (COUNT,">count.txt");
close (COUNT);

open (HISTORY,"history.txt");
@history = ;
close (HISTORY);

open (HISTORY,">history.txt");
print HISTORY "$yesterday|$countn";
foreach $line (@history) {
print HISTORY "$line";
}
close (HISTORY);
}
unless ($oldday =~ /[0-9]/) {
open (DATE,">date.txt");
print DATE "$today";
close (DATE);
}
open (COUNT,"count.txt");
$count = ;
close (COUNT);

chop ($count) if ($count =~ /n$/);
$count++;

open (COUNT,">count.txt");
print COUNT "$count";
close (COUNT);

open (INFO,"info.txt");
@info = ;
close (INFO);

open (INFO,">info.txt");
print INFO "$date|$clock|$ENV{'HTTP_REFERER'}|$ENV{'REMOTE_ADDR'}|$ENV{'REMOTE_HOST'}|$ENV{'HTTP_USER_AGENT'}|$ENV{'REQUEST_URI'}|n";
foreach $line (@info) {
print INFO "$line";
}
close (INFO);
}

sub get_date {
@days = ('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
@months = ('January','February','March','April','May','June','July',
'August','September','October','November','December');

($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
$time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
$year += 1900;

if ($min < 10) {
$min = "0"."$min";
}

$today = ($year*365)+($mon*30)+$mday;
$hour = ($hour - $minus)+$plus;
if ($hour < "0") {
$hour = $hour+24;
$today--;
}
if ($hour > "23") {
$hour = $hour-24;
$today++;
}
$clock = "$hour:$min";
$date = "@months[$mon] $mday";

$mday--;
if ($mday <= "0") {
$test = ($mon/2);
$mday = 30;
if ($test =~ /.|,/) {
$mday = 31;
}
$mon--;
}
if ($mon < "0") {
$mon = 12;
}
$yesterday = "@months[$mon] $mday";
}

sub check {
unless (-e "history.txt") {
open (HISTORY,">history.txt");
close (HISTORY);
}
unless (-e "count.txt") {
open (COUNT,">count.txt");
close (COUNT);
}
unless (-e "info.txt") {
open (COUNT,">info.txt");
close (COUNT);
}
}

sub reset {
open (HISTORY,">history.txt");
close (HISTORY);

open (COUNT,">count.txt");
close (COUNT);

open (COUNT,">info.txt");
close (COUNT);

open (COUNT,">ips.txt");
close (COUNT);

open (COUNT,">date.txt");
close (COUNT);

&admin;
}

sub admin {
if ($INPUT{'password2'}) {
$INPUT{'password'} = $INPUT{'password2'};
}
&check_password;
&get_date;

open (COUNT,"count.txt");
$count = ;
close (COUNT);

open (HISTORY,"history.txt");
@history = ;
close (HISTORY);

foreach $line (@history) {
@stats = split/|/,$line;
if (@stats[0] eq $yesterday) {
$yesterdayhits = @stats[1];
}
}
unless ($yesterdayhits) {
$yesterdayhits = " ";
}

print "Mini logger admin area";
print "
n";

print "
n";
print "nn";
print "
Visitors today$count
Yesterday$yesterdayhits


nn";

print "
";
print "

n";
print "n";

if ($history eq "yes") {
&history;
}
if ($last_refers eq "yes") {
&last_refers;
}
if ($top_refers eq "yes") {
&top_refers;
}
if ($last_visitors eq "yes") {
&last_visitors;
}
if ($top_pages eq "yes") {
&top_pages;
}
if ($search eq "yes") {
&search;
}

open (FILE, "info.txt");
@info = ;
($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat(FILE);
close (FILE);

$size = $size/1000;
print "
Your log file is currently $size kb.
n";
if ($size > $max_log) {
print "Redusing log file...
";
}
if ($size > $max_log) {
foreach $line (@info) {
$lines++;
}
$percent = $max_log/$size;
$lines = $lines*$percent;
$new_max = ($lines/10)*9.99;

open (INFO,">info.txt");
foreach $line (@info) {
unless ($count > $new_max) {
print INFO "$line";
}
$count++;
}
close (INFO);
open (INFO,"info.txt");
($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat(INFO);
close (INFO);

$size = $size/1000;

print "The log file is now $size kb.n";
}
}

###########
# Actions #
###########
#
#
# # #
# # #
###
#

sub last_refers {
&check_password;
&get_date;
open (COUNT,"count.txt");
$count = ;
close (COUNT);

open (INFO,"info.txt");
@info = ;
close (INFO);

open (HISTORY,"history.txt");
@history = ;
close (HISTORY);

$num = 0;
print "
Last 20 referres
n";
print "n";
foreach $line (@info) {
unless ($num > 20) {
@stuff = split/|/,$line;
print "n";
if (@stuff[2]) {
print "n";
}
else {
print "n";
}
}
$num++;
}
print "
@stuff[1]@stuff[2]
No refer


nn";
}

sub top_pages {
$numbertolist = "19";
if ($INPUT{top_pages}) {
print "";
&check_password;
$numbertolist = "39";
}
@info = "";
@refers = "";
@found = "";
@list = "";
@number = "";
@sorted = "";
@stuff = "";
$match = "";
$all = "";
$max = "";
$a = "";
$b = "";

open (INFO,"info.txt");
@info = ;
close (INFO);

foreach $line (@info) {
@stuff = split/|/,$line;
$test = substr(@stuff[6],0,1);
if ($test eq "/") {
@refers[$b] = @stuff[6];
$b++;
}
$all++;
}

@found[0] = "1";
foreach $refer (@refers) {
foreach $refer2 (@found) {
if ($refer eq $refer2) {
$match = 1;
}
}
unless ($match) {
@found[$a] = $refer;
$a++;
}
$match = "";
}

$a = 0;
foreach $refer (@found) {
foreach $refer2 (@refers) {
if ($refer eq $refer2) {
@number[$a]++;
}
}
$a++;
}

$a = 0;
foreach $refer (@found) {
@list[$a] = @number[$a] . " " . $refer;
$a++;
}

@sorted = sort { $b <=> $a }@list;

print "
Top pages
n";
print "n";
foreach $line (@sorted) {
unless ($max > $numbertolist) {
($number, $refer) = split/ /,$line;
$percent = ($number/$all)*100;
$percent =~ s/(d+.d).*/$1/;
print "n";
}
$max++;
}
print "
HitsPercentPage
$number$percent%$refer


";
}

sub top_refers {
&check_password;
if ($INPUT{top_refers}) {
print "";
}
open (INFO,"info.txt");
@info = ;
close (INFO);

foreach $line (@info) {
@stuff = split/|/,$line;
if (@stuff[2] =~ /http:///) {
@refers[$b] = @stuff[2];
$b++;
}
$all++;
}

@found[0] = "1";
foreach $refer (@refers) {
foreach $refer2 (@found) {
if ($refer eq $refer2) {
$match = 1;
}
}
unless ($match) {
@found[$a] = $refer;
$a++;
}
$match = "";
}

$a = 0;
foreach $refer (@found) {
foreach $refer2 (@refers) {
if ($refer eq $refer2) {
@number[$a]++;
}
}
$a++;
}

$a = 0;
foreach $refer (@found) {
@list[$a] = @number[$a] . " " . $refer;
$a++;
}

@sorted = sort { $b <=> $a }@list;

print "
Top referres
n";
print "n";
foreach $line (@sorted) {
unless ($max > 19) {
($number, $refer) = split/ /,$line;
$percent = ($number/$all)*100;
$percent =~ s/(d+.d).*/$1/;
print "n";
}
$max++;
}
print "
HitsPercentURL
$number$percent%$refer


";
}

sub history {
&check_password;
&get_date;
open (COUNT,"count.txt");
$count = ;
close (COUNT);

open (HISTORY,"history.txt");
@history = ;
close (HISTORY);

print "
History
n";
print "n";
foreach $line (@history) {
@stuff = split/|/,$line;
if ($highest < @stuff[1]) {
$highest = @stuff[1];
}
}
if ($highest < $count) {
$highest = $count;
}

unless ($highest eq "") {
$max = 400/$highest;
}
$num = 0;

$width = $max*$count;
$width =~ s/(d+).d.*/$1/;
print "n";
print "n";
print "n";

foreach $line (@history) {
unless ($num > 19) {
@stuff = split/|/,$line;
$width = $max*@stuff[1];
$width =~ s/(d+).d.*/$1/;
print "n";
print "n";
print "n";
}
$num++;
}
print "
$date$count
@stuff[0]@stuff[1]


";
}

sub last_visitors {
&check_password;
open (INFO,"info.txt");
@info = ;
close (INFO);

$num = "0";
print "
Last 20 visitors
n";
print "n";
foreach $line (@info) {
unless ($num > 19) {
@stuff = split/|/,$line;
&findagent;
print "n";
print "n";
print "n";
print "n";
}
$num++;
}
print "
TimeIP addressHost addressUser agent
@stuff[0] - @stuff[1]@stuff[3]@stuff[4]@stuff[5]


nn";
}

sub search {
&check_password;
print "
Search
n";
print "n";
print "Search for
nn";

print "Show
nn";

print "n";
}

sub searchfor {
&check_password;
print "";

open (INFO,"info.txt");
@info = ;
close (INFO);

if ($INPUT{'what'} eq "date") {$what = "0";}
elsif ($INPUT{'what'} eq "clock") {$what = "1";}
elsif ($INPUT{'what'} eq "refer") {$what = "2";}
elsif ($INPUT{'what'} eq "ip") {$what = "3";}
elsif ($INPUT{'what'} eq "host") {$what = "4";}
elsif ($INPUT{'what'} eq "agent") {$what = "5";}

print "

Search result

n";

if ($INPUT{'show'} eq "match") {
foreach $hit (@info) {
@stuff = split/|/,$hit;
unless (@stuff[2]) {
@stuff[2] = "No refer";
}
&findagent;
if (@stuff[$what] eq $INPUT{'searchfor'}) {
$url = @stuff[2];
@stuff[$what] =~ s/$INPUT{'searchfor'}/$INPUT{'searchfor'}/gi;
@result[$a] = "@stuff[0]@stuff[1]@stuff[2]@stuff[3]@stuff[4]@stuff[5]n";
$found++;
$a++;
}
$all++;
}
}
elsif ($INPUT{'show'} eq "half") {
foreach $hit (@info) {
@stuff = split/|/,$hit;
unless (@stuff[2]) {
@stuff[2] = "No refer";
}
&findagent;
if (@stuff[$what] =~ /$INPUT{'searchfor'}/gi) {
$url = @stuff[2];
@stuff[$what] =~ s/$INPUT{'searchfor'}/$INPUT{'searchfor'}/gi;
@result[$a] = "@stuff[0]@stuff[1]@stuff[2]@stuff[3]@stuff[4]@stuff[5]n";
$found++;
$a++;
}
$all++;
}
}
if ($found) {
$percent = ($found/$all)*100;
$percent =~ s/(d+.d).*/$1/;
print "$found matches were found out of $all visitors. That is $percent%.n";
foreach $result (@result) {
unless ($zzz > 19) {
print "$result";
}
$zzz++;
}
print "
n";
}
unless ($found) {
print "Sorry, no matches found.n";
}
}

sub check_password {
unless ($INPUT{'password'} eq "$password") {
print "

Error

The password you entered was incorrect.n";
exit;
}
}

sub getinput {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@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;
$INPUT{$name} = $value;
}

@pairs = split(/&/, $ENV{'QUERY_STRING'});
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$INPUT{$name} = $value;
}
}

sub findagent {
if (@stuff[5] =~ /netscape/gi) {
@stuff[5] = "Netscape";
}
elsif (@stuff[5] =~ /compatible/gi) {
@temp = split/;/,@stuff[5];
if (@temp[2] =~ /win/gi) {
@tempp = split/)/,@temp[2];
@stuff[5] = "@temp[1], "."@tempp[0]";
}
elsif (@temp[3] =~ /win/gi) {
@tempp = split/)/,@temp[3];
@stuff[5] = "@temp[1], "."@tempp[0]";
}
elsif (@temp[4] =~ /win/gi) {
@tempp = split/)/,@temp[4];
@stuff[5] = "@temp[1], "."@tempp[0]";
}
elsif (@temp[5] =~ /win/gi) {
@tempp = split/)/,@temp[5];
@stuff[5] = "@temp[1], "."@tempp[0]";
}
elsif (@stuff[5] =~ /Mac_PowerPC/gi) {
@stuff[5] = "@temp[1], "."Mac_PowerPC";
}
elsif (@stuff[5] =~ /Mac_PowerPPC/gi) {
@stuff[5] = "@temp[1], "."Mac_PowerPPC";
}
elsif (@stuff[5] =~ /Mac_PPC/gi) {
@stuff[5] = "@temp[1], "."Mac_PPC";
}
elsif (@stuff[5] =~ /RISC OS/gi) {
@stuff[5] = "@temp[1], "."RISC OS";
}
elsif (@stuff[5] =~ /WebTV/gi) {
@stuff[5] = "@temp[1], "."WebTV";
}
elsif (@stuff[5] =~ /FDSE/gi) {
@stuff[5] = "Unknown";
}
else {
@stuff[0] = "Unknown";
}
chop (@stuff[5]) if (@stuff[5] =~ /)/);;
}
elsif (@stuff[5] =~ /Win95/gi) {
@stuff[5] = "Windows 95";
}
elsif (@stuff[5] =~ /Win98/gi) {
@stuff[5] = "Windows 98";
}
elsif (@stuff[5] =~ /Win16/gi) {
@stuff[5] = "Windows 16";
}
elsif (@stuff[5] =~ /WinNT/gi) {
@stuff[5] = "Windows NT";
}
elsif (@stuff[5] =~ /Linux/gi) {
@stuff[5] = "Linux";
}
elsif (@stuff[5] =~ /Macintosh/gi) {
@stuff[5] = "Macintosh";
}
elsif (@stuff[5] =~ /AOLpress/gi) {
@stuff[5] = "AOLpress";
}
elsif (@stuff[5] =~ /FreeBSD/gi) {
@stuff[5] = "FreeBSD";
}
elsif (@stuff[5] =~ /SunOS/gi) {
@stuff[5] = "SunOS";
}
else {
@stuff[5] = "Unknown";
}
}

-------------------------------------

The only thing it doesn't do is display the total no. of hits!! This must be really easy to do for a .cgi programmer. Could someone please do this??? I can reward !

THANKS VERY MUCH,
James Smith

(webmaster hiphopmusic.co.uk)
© UBB.Developers