Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a CGI Script to Log website activity

Status
Not open for further replies.

naiku

Technical User
Apr 25, 2002
346
US
Hi all

I am completely new to CGI and so have no idea how to use it. When I purchased my webspace it came with 3 free scripts, one of them being a log script for monitoring activity on my website. However I have no idea how to use the script (Copied below) Do I need to change anything to use it properly? how do I add it to my site and how do I get the information back from the log. Sorry if all these seems really stupid, but hey we all gotta learn somewhere :p

CGI Script as follows (This came free with my webspace)

Code:
#!/usr/bin/perl
#
# "Book 'em, Dan-O" Logger Script by Spider
# Created 9 July 1996
# Email me at  spider@servtech.com
# [URL unfurl="true"]http://www.servtech.com/public/spider[/URL]
#
# Secret Decoder Ring - aka Organization of Log File
# Time Stamp / Person\Machine / Referring URL / Browser Used
# 
# This script can be run as a SSI or used 
# in a "redirect" fashion via *normal* CGI calls.

########## Set Variables ############

$SSI = 1;  
# 0 if not used as a SSI  -    1 if used as a SSI


$logfile = "../../logs/log.dat";

$exclude = 0; 
# 1 if you want to exclude YOUR IP/Domain/Machine Name  - 0 
otherwise

$my_addr = "your.machine.name";  
# used with the "exclude" portion

$HomeDirURL = "[URL unfurl="true"]http://www.nameme.co.uk/";[/URL]
# change this if you're not using SSI's

$nextfile = "file.html";  
# again, change if you're not using SSI's 

########## So much for that.. On with the show! #######

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);

    # Un-Webify plus signs and %-encoding
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    # Stop people from using subshells to execute commands
    # Not a big deal when using sendmail, but very important
    # when using UCB mail (aka mailx).
    $value =~ s/~!/ ~!/g; 

    # Uncomment for debugging purpose
     print &quot;Setting $name to $value<P>&quot;;
    $FORM{$name} = $value;
}

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = 
localtime(time);

if ($sec < 10) {
	$sec = &quot;0$sec&quot;;
}
if ($min < 10) {
	$min = &quot;0$min&quot;;
}
if ($hour < 10) {
	$hour = &quot;0$hour&quot;;
}
if ($mon < 10) {
	$mon = &quot;0$mon&quot;;
}
if ($mday < 10) {
	$mday = &quot;0$mday&quot;;
}

$month = ($mon + 1);
@months = 
(&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;,&quot;A
ugust&quot;,&quot;September&quot;,&quot;October&quot;,&quot;November&quot;,&quot;December&quot;);
$year += 1900;
$date = &quot;$hour\:$min\:$sec $month/$mday/$year&quot;;

# Now that we know what the time/date is.. let's have fun

if ($SSI == 1) {
	if ($exclude == 1) {
		&log unless ($ENV{'REMOTE_HOST'} eq $my_addr);
	} else {
	&log;
	}
	exit;
}

if ($SSI == 0) {
	if ($exclude == 1) {
		&log unless ($ENV{'REMOTE_HOST'} eq $my_addr);
	} else {
	&log;
	}
	&redir;
	exit;
}

sub log {
	
	if (! open(LOG,&quot;>>$logfile&quot;)) {
		print &quot;Content-type: text/html\n\n&quot;;
		print &quot;Couldn't open $logfile so I'm 
bugging out..\n&quot;;

		print &quot;At $date, $ENV{'REMOTE_HOST'} came 
here from $ENV{'HTTP_REFERER'} using $ENV
{'HTTP_USER_AGENT'}.\n&quot;;


		exit;
	}
	print LOG &quot;At $date, $ENV{'REMOTE_HOST'} came here 
from $ENV{'HTTP_REFERER'} using $ENV{'HTTP_USER_AGENT'}.\n&quot;;
	close (LOG);
}

sub redir {
	print &quot;Location: $HomeDirURL$nextfile\n\n&quot;;
}

Sorry the post is so long but any help is really appreciated, Thanks

naiku
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top