perl question
laura hudson
hudsonl at ouvaxa.cats.ohiou.edu
Fri Aug 22 09:49:12 EDT 1997
Hi folks,
As I mentioned in an earlier message, we at Ohio University are moving to a
Web front-end for most of our patron terminals. We've been given some perl
cgi scripts from people on this list-serv to help with IP address validation
(so that people can not get to a database which is licensed for on-campus
use from off-campus) and to make a log file so that we can keep statistics
on how many people access each database.
I've decided to put two scripts together using an if/else structure, so that
first the IP address is checked, and then if it is on-campus, the patron is
redirected to the database and an entry is made in the log file. If IP
address is incorrect, they will get an error message and no mark will be
made in the log.
I am still a (very) beginning perl scripter and am having trouble getting
this script to work correctly. At present, it passes the user to the
database and marks the log file regardless of the users IP address.
Does anybody out there have the expertise and the time to take a look at my
script and see if there is some error which would cause this? I've included
it, with comments, below.
Best to reply to me privately, I think.
Thanks in advance,
Laura Hudson
Alden Library Reference Department
Ohio University Libraries
lhudson1 at ohiou.edu
&GetInst($ENV{'REMOTE_ADDR'});
#this script has been modified for an nt server. to run this script, put it
in your cgi folder,
#then point at it from your browser like this: <http:// . . .
redirect.pl?http://url-you-want-
#to-be-passed-to>
#the log file that the script will create if the ip address is correct.
rename this for your
#own path
$curlog = "f:\\temp\\redirect.txt";
#splits up the ip address into four variables for our log file (we just
happen to want it that
#way).
$ip = "$ENV{'REMOTE_ADDR'}";
($IP1, $IP2, $IP3, $IP4) = split /\./, $ip;
#the delimiters to be used between fields in the log file
$delim = "\n";
$field_sep = "\t";
# Code to produce string which is date and time
($sec, $min, $hour, $mday, $mon, $year) = localtime( time );
$mday = '0' . $mday if (length( $mday ) < 2);
$TimeOnly = sprintf("%02d:%02d:%02d", $hour, $min, $sec);
$month = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon];
$DateOnly = $month."/".$mday."/"."19".$year;
$tzone = "-0800";
$logdate =" ".$DateOnly." ".$TimeOnly." ";
#if the ip address is correct, make a notation in the log file and run sub
re_direct
if ($in {'user_inst'} = "internal")
{
&re_direct( "$ENV{'QUERY_STRING'}" );
open( FILE, ">>$curlog" );
print FILE $IP1, $field_sep, $IP2, $field_sep, $IP3, $field_sep, $IP4,
$field_sep, $logdate, $field_sep, "$ENV{'QUERY_STRING'}", $delim;
close( FILE );
}
#if the ip address is wrong, give them an error message
else
{
print "Content type=text\n\n";
print "your ip address is incorrect";
}
#the ip address checker--the address pattern will need to be turned into a
pattern match after #you modify it to check against your own machine.
sub GetInst {
local ($user_ip);
$user_ip = "$_[0]";
if ($user_ip =~ /^132\.235\.63\.118/) {
$user_inst = "internal";
}
else {
$user_inst = "external";
}
return $user_inst;
}
#the part that redirects the user to the url listed on the link after the "?"
sub re_direct
{
local ($location) = @_;
print <<"--end--";
Content-type: text/html
Location: $location
<h1>301 Redirect</h1>
Document is located at <a href="$location">$location</a>
--end--
}
More information about the Web4lib
mailing list