Retrieving IP with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rabloo
Forum Newbie
Posts: 4
Joined: Wed Apr 21, 2010 5:53 pm

Retrieving IP with PHP

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Retrieving IP with PHP

Post 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.
Rabloo
Forum Newbie
Posts: 4
Joined: Wed Apr 21, 2010 5:53 pm

Re: Retrieving IP with PHP

Post by Rabloo »

Thanks.
User avatar
Architek
Forum Commoner
Posts: 44
Joined: Wed Jul 01, 2009 5:01 am
Location: Portland OR

Re: Retrieving IP with PHP

Post 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;
    	}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Retrieving IP with PHP

Post 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.
Post Reply