Problem finding the users local IP address

Hello,

I’m working on a script to pull in the users local IP address so that in turn I can use the ip address to find the users location. I found the following script which should pull in the userslocal IP address but its not working.

Results from the script below: ::1

what am I doing wrong?


function get_ip_address()
{	foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') 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;	}
			}
		}
	}
}


I hust tried your script both localhost and onlie and it works fine:



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 '$_SERVER["' .$key .'"]  = ' .$ip;
        } 
      } 
    } 
  } 
}#endfunc

echo get_ip_address();


And no doubt you will be delighted to know that Google Webmaster’s Tools->Health->Fetch as Google:

Server address is: $_SERVER[“REMOTE_ADDR”] = 66.249.75.36

thanks for the replay back.
sorry for the confusion, when i copy/pasted the post i forgot to include the call to the function.
problem still there…

Do a var_dump($_SERVER) see what you are getting for those items. Chances are something before REMOTE_ADDR is returning a value.

Hello cpradio,
I dont see the ip address anywhere.

parts of var_dump:

Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7 Server at localhost Port 80
" 
["SERVER_SOFTWARE"]=> string(45) "Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7" 
["SERVER_NAME"]=> string(9) "localhost" 
["SERVER_ADDR"]=> string(3) [B]"::1"[/B] 
["SERVER_PORT"]=> string(2) "80" 
["REMOTE_ADDR"]=> string(3) "::1" 
["DOCUMENT_ROOT"]=> string(15) "C:/xampp/htdocs" 
["REQUEST_SCHEME"]=> string(4) "http" 
["CONTEXT_PREFIX"]=> string(0) "" 
["CONTEXT_DOCUMENT_ROOT"]=> string(15) "C:/xampp/htdocs" 
["SERVER_ADMIN"]=> string(20) "postmaster@localhost" 
["SCRIPT_FILENAME"]=> string(40) "C:/xampp/htdocs/geocoding/geotesting.php" 
["REMOTE_PORT"]=> string(5) "53878" 
["GATEWAY_INTERFACE"]=> string(7) "CGI/1.1" 
["SERVER_PROTOCOL"]=> string(8) "HTTP/1.1" 
["REQUEST_METHOD"]=> string(4) "POST" 
["QUERY_STRING"]=> string(0) "" 
["REQUEST_URI"]=> string(25) "/geocoding/geotesting.php" 
["SCRIPT_NAME"]=> string(25) "/geocoding/geotesting.php" 
["PHP_SELF"]=> string(25) "/geocoding/geotesting.php" 
["REQUEST_TIME_FLOAT"]=> float(1365702806.399) 
["REQUEST_TIME"]=> int(1365702806) }

Im using XAMPP to test my script… could xampp be the cause of the problem?

Please format your script using PHP tags:

also I have this function which formats array output:



//----------------------
//
//  typical usage:
//           vd( $_SERVER );
// 
//----------------------
function vd( $a=array(), $lVarDump=FALSE )
{
  echo '<pre>';
    if($lVarDump):
     var_dump($a);
   else:
     print_r($a);
   endif;
  echo '</pre>';


Fron your var_dump($_SERVER) results [“REMOTE_ADDR”]=> string(3) “::1”

so the function is reporting correctly.

Have you tested the script online?

I am using XAMPP Lite.

You have apache setup to listen to IPv6, which ::1 means local, which is correct if you are running this local.

To configure apache to ONLY listen to IPv4, see this thread: http://stackoverflow.com/questions/2939311/remote-addr-not-returning-ipv4-address

Alright, I’m going to lay some things down. ONLY use “REMOTE_ADDR” to get a user’s IP (except when your server is behind a known proxy). Use NOTHING else.

This is all your get_ip_address function should do:

function get_ip_address () { return $_SERVER['REMOTE_ADDR']; }

That is all.

Now, when you are accessing the script locally via localhost the IP will always be either ::1 or 127.0.0.1 ALWAYS.