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!

Get a user's IP Address

Status
Not open for further replies.

mtorbin

Technical User
Nov 5, 2002
369
US
Hey all, I'm working on a new script to get a user's IP address. However, I'm getting errors with the following and I'm not sure why:

CODE:

#! /usr/bin/perl -wT

$remote_ip = $ENV[REMOTE_ADDR];

print $remote_ip;


ERRORS:

Premature end of script headers: findIP.cgi
Argument "REMOTE_ADDR" isn't numeric in array element at ...findIP.cgi line 3.
Use of uninitialized value in print at ...findIP.cgi line 5.

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Change this line:

Code:
$remote_ip = $ENV[REMOTE_ADDR];

to this:

Code:
$remote_ip = $ENV{'REMOTE_ADDR'};

The environment variables are stored in a hash, so you need to use curly braces to get to the data within.

- Rieekan
 
Thank you Rieekan (just to update you... the project reqs have changed... LOL..such is life).

So I have that much I put in a split function as such:

CODE:

#! /usr/bin/perl -wT
print "Content text:html/text\n\n";

$userAddr=$ENV{REMOTE_ADDR};

#print $userAddr;

@fields = split /./, $userAddr;

But when I use this, I get the following error:

ERROR:

Name "main::fields" used only once: possible typo at /var/ line 8.

Why is that? I clearly declared the array in the @fields line, right?

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Grrr.... OK, this is what I have thus far and the error log is only giving me "premature end of script"

CODE:

\#! /usr/bin/perl -wT
print "Content text:html/text\n\n";

$userAddr=$ENV{REMOTE_ADDR};

#print $userAddr;

@ipParts = split ( /./, $userAddr);

print "@ipParts";


- MT



Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Ok, to update you, this is what I have thus far:

CODE:

#! /usr/bin/perl -wT
print "Content text:html/text\n\n";

$userAddr=$ENV{REMOTE_ADDR};

@ipParts = split ( /./, $userAddr);

print "@ipParts[3]";
print "If nothing else, I will print this line.";


The problem is that the
print "@ipParts[3]"; doesn't do jack. I get an error about rephrasing it to
print $ipParts[3]"; and when I do that it then tells me that the variable is uninitialized. So I try to initialize it and it then tells me about having a useless variable in there. Grrr.... OK, where I am I going wrong?

- MT



Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Ok, I'm on the last part. Here's what I have thus far:

CODE:

#! /usr/bin/perl -wT
print "Content text:html/text\n\n";

$userAddr=$ENV{REMOTE_ADDR};
$mattIP = "35";

@ipParts = split ( /\./, $userAddr);

$mySection=$ipParts[3];

if ($mySection=$mattIP) {
print "HOT DOGGIE!!!";
}

else {
print "Um... no.";
}

This works perfectly (finally)! Now all I have to do is replace those print commands with redirection. Any ideas?

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
OK, I got it all working. Here is the final code:

#! /usr/bin/perl -wT
#print "Content text:html/text\n\n";

$userAddr=$ENV{REMOTE_ADDR};
$mattIP = "35";

@ipParts = split ( /\./, $userAddr);

$mySection=$ipParts[3];

if ($mySection=$mattIP) {
#print "HOT DOGGIE!!!";
print "Location: }

else {
print "Um... no.";
}

If there are any improvements that can be made, please let me know.

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Matt,

This will work fine on a static network, ie fixed IP addresses, but if its DHCP, or telecomms w/o fixed IP, you're gonna be in a world of hurt.

$ENV{$REMOTE_HOST} is more dependable, but both are potential threats v.v. spoofing, and REMOTE_HOST isn't always populated

--Paul
 
Paul,

Thanks for the heads up, I might come back to you on this on some later date. Thankfully, however, this is a closed network (i.e. our Intranet).

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Shouldn't this...

Code:
#! /usr/bin/perl -wT
print "Content text:html/text\n\n";

be...

Code:
#! /usr/bin/perl -wT
print [b][red]"Content-type: text/html\n\n";[/red][/b]


Kind Regards
Duncan
 
duncdude, yes it should.

mtorbin,
Code:
$userAddr=$ENV{REMOTE_ADDR};
should be
Code:
$userAddr=$ENV{[COLOR=red]'[/color]REMOTE_ADDR[COLOR=red]'[/color]};
(Double quotes would work too).

Furthermore
Code:
if ($mySection=$mattIP) {
should be
Code:
if ($mySection[COLOR=red]==[/color]$mattIP) {
At the moment you're assigning $mySection the value of $mattIP and testing whether it was successful (always true). You need to use the == operator to test for (numeric) equality.

-- Chris Hunt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top