By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,932 Members | 1,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,932 IT Pros & Developers. It's quick & easy.

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

P: 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.
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
*** deko escribi/wrote (Sat, 30 Oct 2004 17:34:24 GMT):
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 --


$_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 informticas recibidas por correo irn directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--
Jul 17 '05 #2

P: n/a
> $_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.


Ah, I see. Thanks for the help!
Jul 17 '05 #3

P: n/a
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');
}
}
Jul 17 '05 #4

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


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 informticas recibidas por correo irn directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions
--
Jul 17 '05 #5

P: n/a
"deko" <ww*******************************@nospam.com> wrote:
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();
}

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, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 17 '05 #6

P: n/a
On Sun, 31 Oct 2004 14:39:17 -0800, Tim Roberts <ti**@probo.com>
wrote:
"deko" <ww*******************************@nospam.com> wrote:
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();
}

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.

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
================================================== =========================
Jul 17 '05 #7

P: n/a
"deko" <ww*******************************@nospam.com> wrote in message news:<Ag*******************@newssvr21.news.prodigy .com>...
<snip>
Is there a way to get only the originating IP?


<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
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.