Page 1 of 1
Retrieving IP with PHP
Posted: Wed Apr 21, 2010 6:01 pm
by Rabloo
When using either $_SERVER['REMOTE_ADDR']; or @$REMOTE_ADDR;, I understand this retrieves the IP of the visitor of the page.
I'm just wondering, if I'm on a LAN network and I visit the page on both computers, will they have different IP's when based on the return value of
$_SERVER['REMOTE_ADDR']; or @$REMOTE_ADDR ?
Does each computer have a unique IP no matter what?
Re: Retrieving IP with PHP
Posted: Wed Apr 21, 2010 7:47 pm
by requinix
No. REMOTE_ADDR will get you the public-facing IP address. When you're behind a router you'll get the router's address.
And no, you can't get the address of the machine behind it.
Re: Retrieving IP with PHP
Posted: Wed Apr 21, 2010 9:35 pm
by Rabloo
Thanks.
Re: Retrieving IP with PHP
Posted: Thu Apr 22, 2010 2:03 am
by Architek
I was actually looking for something of the same nature the other day for an internal site I am building and wanted to capture subnets etc...
I have not tried it yet but will be doing so later...
here is the URL
http://www.cyberciti.biz/faq/php-howto- ... erbrowser/
Geert March 22, 2009 at 1:38 pm
They are all behind the same proxy-server. In that case $_SERVER['REMOTE_ADDR'] provides you with the ip-address of the proxy-server.
This function will determine the real ip-adress:
Code: Select all
function ipCheck() {
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}
elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}
elseif (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
Re: Retrieving IP with PHP
Posted: Thu Apr 22, 2010 4:55 pm
by John Cartwright
Architek wrote:I was actually looking for something of the same nature the other day for an internal site I am building and wanted to capture subnets etc...
I have not tried it yet but will be doing so later...
here is the URL
http://www.cyberciti.biz/faq/php-howto- ... erbrowser/
Geert March 22, 2009 at 1:38 pm
They are all behind the same proxy-server. In that case $_SERVER['REMOTE_ADDR'] provides you with the ip-address of the proxy-server.
This function will determine the real ip-adress:
Code: Select all
function ipCheck() {
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}
elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}
elseif (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
That function will only reveal the visitors ip (behind a proxy) if the proxy is not transparent -- which most are. Simply put, when a user is behind a proxy, it is usually not possible to get their real ip address.