Connecting Tech Pros Worldwide Help | Site Map

Why is $_SERVER["REMOTE_ADDR"] returning multiple IP Addresses?

  #1  
Old July 17th, 2005, 10:22 AM
deko
Guest
 
Posts: n/a
Why is $_SERVER["REMOTE_ADDR"] returning multiple IP Addresses?

Actually, I'm not sure if it's $_SERVER["REMOTE_ADDR"] -- or which if/else
statement -- that's the problem, but what I'm getting as a value for $visip
looks like this:

172.16.42.181, 62.138.35.94

Why am I getting more than one IP Address? Which IP is the originating IP
Address? Is there a way to get only the originating IP?

Here's the code:


if (isset($_SERVER))
{
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
$visip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif (isset($_SERVER["HTTP_CLIENT_IP"]))
{
$visip = $_SERVER["HTTP_CLIENT_IP"];
}
else
{
$visip = $_SERVER["REMOTE_ADDR"];
}
}
else
{
if (getenv('HTTP_X_FORWARDED_FOR'))
{
$visip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_CLIENT_IP'))
{
$visip = getenv('HTTP_CLIENT_IP');
}
else
{
$visip = getenv('REMOTE_ADDR');
}
}

You can view the page that displays the results here:

http://www.clearpointsystems.com/stats.php

(scroll to entry for Oct 29 2004 10:17 am and Oct 30 2004 05:26 am)

Thanks in advance.


  #2  
Old July 17th, 2005, 10:22 AM
Alvaro G. Vicario
Guest
 
Posts: n/a

re: Why is $_SERVER["REMOTE_ADDR"] returning multiple IP Addresses?


*** deko escribió/wrote (Sat, 30 Oct 2004 17:34:24 GMT):[color=blue]
> Why is $_SERVER["REMOTE_ADDR"] returning multiple IP Addresses?
>
> Actually, I'm not sure if it's $_SERVER["REMOTE_ADDR"] -- or which if/else
> statement --[/color]

$_SERVER["HTTP_X_FORWARDED_FOR"] is a comma-separated list of IP addresses,
though it's normally a one element list. This variable is the result of
intermediate proxies.

This function gets the first element:

function real_ip(){
if($tmp=$_SERVER['HTTP_X_FORWARDED_FOR']){
$tmpip=explode(",", $tmp);
return trim($tmpip[0]);
}else return $_SERVER['REMOTE_ADDR'];
}


--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--
  #3  
Old July 17th, 2005, 10:22 AM
deko
Guest
 
Posts: n/a

re: Why is $_SERVER["REMOTE_ADDR"] returning multiple IP Addresses?


> $_SERVER["HTTP_X_FORWARDED_FOR"] is a comma-separated list of IP
addresses,[color=blue]
> though it's normally a one element list. This variable is the result of
> intermediate proxies.[/color]

Ah, I see. Thanks for the help!


  #4  
Old July 17th, 2005, 10:22 AM
deko
Guest
 
Posts: n/a

re: Why is $_SERVER["REMOTE_ADDR"] returning multiple IP Addresses?


Follow up question:

How do I call the RealIp function? What I have below is not working.

function realIp()
{
$ip_array=explode(",", $visip);
return trim($ip_array[0]);
}
if (isset($_SERVER))
{
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
$visip = $_SERVER["HTTP_X_FORWARDED_FOR"];
$visip = realIp();
}
elseif (isset($_SERVER["HTTP_CLIENT_IP"]))
{
$visip = $_SERVER["HTTP_CLIENT_IP"];
}
else
{
$visip = $_SERVER["REMOTE_ADDR"];
}
}
else
{
if (getenv('HTTP_X_FORWARDED_FOR'))
{
$visip = getenv('HTTP_X_FORWARDED_FOR');
$visip = realIp();
}
elseif (getenv('HTTP_CLIENT_IP'))
{
$visip = getenv('HTTP_CLIENT_IP');
}
else
{
$visip = getenv('REMOTE_ADDR');
}
}


  #5  
Old July 17th, 2005, 10:22 AM
Alvaro G. Vicario
Guest
 
Posts: n/a

re: Why is $_SERVER["REMOTE_ADDR"] returning multiple IP Addresses?


*** deko escribió/wrote (Sat, 30 Oct 2004 19:33:01 GMT):[color=blue]
> function realIp()
> {
> $ip_array=explode(",", $visip);
> return trim($ip_array[0]);
> }[/color]

I don't know what your code tries to do but if you want to use variables
that are external to the function you must either:

1) Pass them as parameters:

function realIp($visip)
{
...
}


2) Declare them as global:

funtion realIp()
{
global $visip;
...
}


--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--
  #6  
Old July 17th, 2005, 10:23 AM
Tim Roberts
Guest
 
Posts: n/a

re: Why is $_SERVER["REMOTE_ADDR"] returning multiple IP Addresses?


"deko" <www-dot-clearpointsystems-dot-com@nospam.com> wrote:
[color=blue]
>Follow up question:
>
>How do I call the RealIp function? What I have below is not working.
>
>function realIp()
> {
> $ip_array=explode(",", $visip);
> return trim($ip_array[0]);
> }
>if (isset($_SERVER))
> {
> if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
> {
> $visip = $_SERVER["HTTP_X_FORWARDED_FOR"];
> $visip = realIp();
> }[/color]


Don't rely on globals.

function realIp($unrealIp)
{
$ip_array = explode(",", $unrealIp);
return trim($ip_array[0]);
}
....
{
$visip = realIp($_SERVER['HTTP_X_FORWARDED_FOR"]);
}

What makes you think the first IP in the list is any more "real" than any
of the others? Do you hope to use this for some kind of validation? If
so, the numbers are quite unreliable.
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
  #7  
Old July 17th, 2005, 10:23 AM
Gary L. Burnore
Guest
 
Posts: n/a

re: Why is $_SERVER["REMOTE_ADDR"] returning multiple IP Addresses?


On Sun, 31 Oct 2004 14:39:17 -0800, Tim Roberts <timr@probo.com>
wrote:
[color=blue]
>"deko" <www-dot-clearpointsystems-dot-com@nospam.com> wrote:
>[color=green]
>>Follow up question:
>>
>>How do I call the RealIp function? What I have below is not working.
>>
>>function realIp()
>> {
>> $ip_array=explode(",", $visip);
>> return trim($ip_array[0]);
>> }
>>if (isset($_SERVER))
>> {
>> if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
>> {
>> $visip = $_SERVER["HTTP_X_FORWARDED_FOR"];
>> $visip = realIp();
>> }[/color]
>
>
>Don't rely on globals.
>
>function realIp($unrealIp)
>{
> $ip_array = explode(",", $unrealIp);
> return trim($ip_array[0]);
>}
>...
> {
> $visip = realIp($_SERVER['HTTP_X_FORWARDED_FOR"]);
> }
>
>What makes you think the first IP in the list is any more "real" than any
>of the others? Do you hope to use this for some kind of validation? If
>so, the numbers are quite unreliable.[/color]


Exactly. If there's more than one IP, you'd have to validate for each
one or all of them at once.



--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://signup.databasix.com
================================================== =========================
  #8  
Old July 17th, 2005, 10:24 AM
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a

re: Why is $_SERVER["REMOTE_ADDR"] returning multiple IP Addresses?


"deko" <www-dot-clearpointsystems-dot-com@nospam.com> wrote in message news:<AgQgd.36818$QJ3.33665@newssvr21.news.prodigy .com>...
<snip>[color=blue]
> Is there a way to get only the originating IP?[/color]

<snip>

Try i2c_realip() available at
<http://in2.php.net/source.php?url=/include/ip-to-country.inc>

--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com
Closed Thread