[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Server-side includes and IP adresses



>Hi there,
>
>I'm looking for a way to get a perl script server-side include to get
>the IP adress of the person loading the page. Is this possible? If so,
>how? And anyways, how do I get the IP adress of someone running a script?
there are a couple of ways -

one is through CGI.pm - look up "fetching environment variables"
if you call the 'remote_host()' function you should get an URL or an IP adress

the other (that I know of - TMTOWTDI) is to roll your own cgi that reads the environment variables, I prefer this method because I find it slightly more flexible than the CGI.pm interface to these values. The script below just prints out all the variables and their values, but it will give you an idea if what is created when you initiate a CGI request

__begin__

#! perl -w

#=========== declare includes =============

use strict;
use diagnostics-verbose;

#========== declare variables =============

my($header,$body,$tail);

#============= script body ================


$header='<HTML><HEAD><TITLE>What the server creates to handle a CGI transaction</TITLE></HEAD>';
$body='<BODY BGCOLOR="#FFFFFF" TEXT="#000000">';
$tail= '<BR><BR><BR><A HREF="../index.html">Back to the index</A></BODY></HTML>';


print $header,$body,"<H3>The list is as follows:</H3> (data in red)<P>";



for (%ENV) {
print $_,' : <FONT COLOR="&#FF0000">',$ENV{$_},'</FONT><P>' if $ENV{$_};
}

print $tail;
__end__


This next one shows how you can filter the Environment Variables and use the info gleaned for whatever purposes you want (which is pretty much how companies like DOUBLECLICK make their living)-

__begin__
#! perl -w

#=========== declare includes =============

#use strict;
use diagnostics-verbose;

#========== declare variables =============
my ($header,$body,$tail,$language);
my ($browser,$OS);
#============= script body ================
$header='<HTML><HEAD><TITLE>What we already know about you.....</TITLE></HEAD>';
$body='<BODY BGCOLOR="#FFFFFF" TEXT="#000000">';
$tail= '<BR><BR><BR><A HREF="../index.html">Back to the index</A></BODY></HTML>';

print $header,$body;

if ($ENV{'HTTP_ACCEPT_LANGUAGE'} eq 'ja'){
$language="Japanese";
} elsif ($ENV{'HTTP_ACCEPT_LANGUAGE'} eq 'en'){
$language="English";
}

print "You speak $language<p>" if $language;


if ($ENV{'HTTP_USER_AGENT'} =~/MSIE/){
$browser='Internet Explorer';
}elsif ($ENV{'HTTP_USER_AGENT'} =~/I; PPC/){
$browser='Netscape Navigator';
} else {
$browser=$ENV{'HTTP_USER_AGENT'};
}




if ($ENV{'HTTP_USER_AGENT'}=~/Mac/){
$OS='Macintosh';
}



print "You use $browser as your browser<p>" if $browser;
print "You\'re using a $OS computer <p>" if $OS;
print 'You were looking at this page: <b>',$ENV{'HTTP_REFERER'},'</b><p>'if $ENV{'HTTP_REFERER'} ;
print 'You\'re connected to the internet at: <b>',$ENV{'REMOTE_ADDR'},'</b><p>' if $ENV{'REMOTE_ADDR'};
print $tail;
__end__
#========= sub routine separator ===========