How to retrive the correct IP address for locaino information

hello everybody,

I’m working on application which uses the IP address of the device to identify location information such as city, state, country. The script works but its picking up the IP address that’s assigned by the router.

how can I get the actually ip address that I need to use in my script to get the correct location information?

Current setup I have is the internet cable box which is connected to a wireless router and my laptop receives the IP address from the wireless router as such 192.168.0.94.

I use an external site for retrieve the location information => http://www.geoplugin.net

And the script I use in my application to get the IP address.


function get_ip_address()
{	$aa = array
	  ( 'HTTP_CLIENT_IP',
		'HTTP_X_FORWARDED_FOR',
		'HTTP_X_FORWARDED',
		'HTTP_X_CLUSTER_CLIENT_IP',
		'HTTP_FORWARDED_FOR',
		'HTTP_FORWARDED',
		'REMOTE_ADDR'
	  );
	  foreach ( $aa as $key)
	  {	if(array_key_exists($key, $_SERVER) === true)
		{	foreach (explode(',', $_SERVER[$key]) as $ip)
			{	if (filter_var($ip, FILTER_VALIDATE_IP) !== false)
				{	return $ip; }
			}
		}
	  }
}

Use $_SERVER[‘REMOTE_ADDR’]. The reason you get a router-assigned IP is because you’re connecting to a local server - probably your own computer. You wont get an external IP that way; it’s simply not transmitted. Host the script up on an external site and connect to it, and you’ll get your external IP.

thanks StarLion,

Ill try that…
r

Only ever use $_SERVER[‘REMOTE_ADDR’]! I wish the these “get_ip_address” functions would just die. You should never use anything but ‘REMOTE_ADDR’. Unless your server is behind a KNOWN proxy (or load blancer) itself that is properly configured.