使用PHP获取客户端IP地址

我想获取使用我的网站的客户端IP地址。 我正在使用PHP $_SERVER超全局:

 $_SERVER['REMOTE_ADDR']; 

但我看到它不能给这个正确的IP地址。 我得到我的IP地址,看到它是不同于我的IP地址,我也可以看到我的IP地址在一些网站,如:

 http://whatismyipaddress.com/ 

我粘贴给我的PHP函数的IP地址,但是这个网站没有显示这个结果。 这个问题是如何发生的?我如何得到客户端的IP地址?

获取访问者/客户端IP地址的最简单方法是使用$_SERVER['REMOTE_ADDR']$_SERVER['REMOTE_HOST']variables。

但是,有时这不会返回访问者的正确IP地址,所以我们可以使用其他一些服务器variables来获取IP地址。

下面的两个函数只有在如何以及从哪里获取值的差异是等价的。

getenv()用于获取PHP中的环境variables的值。

 // Function to get the client IP address function get_client_ip() { $ipaddress = ''; if (getenv('HTTP_CLIENT_IP')) $ipaddress = getenv('HTTP_CLIENT_IP'); else if(getenv('HTTP_X_FORWARDED_FOR')) $ipaddress = getenv('HTTP_X_FORWARDED_FOR'); else if(getenv('HTTP_X_FORWARDED')) $ipaddress = getenv('HTTP_X_FORWARDED'); else if(getenv('HTTP_FORWARDED_FOR')) $ipaddress = getenv('HTTP_FORWARDED_FOR'); else if(getenv('HTTP_FORWARDED')) $ipaddress = getenv('HTTP_FORWARDED'); else if(getenv('REMOTE_ADDR')) $ipaddress = getenv('REMOTE_ADDR'); else $ipaddress = 'UNKNOWN'; return $ipaddress; } 

$ _SERVER是一个包含由Web服务器创build的服务器variables的数组。

 // Function to get the client IP address function get_client_ip() { $ipaddress = ''; if (isset($_SERVER['HTTP_CLIENT_IP'])) $ipaddress = $_SERVER['HTTP_CLIENT_IP']; else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; else if(isset($_SERVER['HTTP_X_FORWARDED'])) $ipaddress = $_SERVER['HTTP_X_FORWARDED']; else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; else if(isset($_SERVER['HTTP_FORWARDED'])) $ipaddress = $_SERVER['HTTP_FORWARDED']; else if(isset($_SERVER['REMOTE_ADDR'])) $ipaddress = $_SERVER['REMOTE_ADDR']; else $ipaddress = 'UNKNOWN'; return $ipaddress; } 

在PHP 5.3或更高版本中,你可以这样得到它:

 $ip = getenv('HTTP_CLIENT_IP')?: getenv('HTTP_X_FORWARDED_FOR')?: getenv('HTTP_X_FORWARDED')?: getenv('HTTP_FORWARDED_FOR')?: getenv('HTTP_FORWARDED')?: getenv('REMOTE_ADDR'); 
  $ipaddress = ''; if ($_SERVER['HTTP_CLIENT_IP'] != '127.0.0.1') $ipaddress = $_SERVER['HTTP_CLIENT_IP']; else if ($_SERVER['HTTP_X_FORWARDED_FOR'] != '127.0.0.1') $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; else if ($_SERVER['HTTP_X_FORWARDED'] != '127.0.0.1') $ipaddress = $_SERVER['HTTP_X_FORWARDED']; else if ($_SERVER['HTTP_FORWARDED_FOR'] != '127.0.0.1') $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; else if ($_SERVER['HTTP_FORWARDED'] != '127.0.0.1') $ipaddress = $_SERVER['HTTP_FORWARDED']; else if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1') $ipaddress = $_SERVER['REMOTE_ADDR']; else $ipaddress = 'UNKNOWN'; 

以下是使用本地和局域网IP地址filter获取IP地址的function:

 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 $IPaddress){ $IPaddress = trim($IPaddress); // Just to be safe if (filter_var($IPaddress, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) { return $IPaddress; } } } } } 

q

它也适用于内部IP地址:

  function get_client_ip() { $ipaddress = ''; if (getenv('HTTP_CLIENT_IP')) $ipaddress = getenv('HTTP_CLIENT_IP'); else if(getenv('HTTP_X_FORWARDED_FOR')) $ipaddress = getenv('HTTP_X_FORWARDED_FOR'); else if(getenv('HTTP_X_FORWARDED')) $ipaddress = getenv('HTTP_X_FORWARDED'); else if(getenv('HTTP_FORWARDED_FOR')) $ipaddress = getenv('HTTP_FORWARDED_FOR'); else if(getenv('HTTP_FORWARDED')) $ipaddress = getenv('HTTP_FORWARDED'); else if(getenv('REMOTE_ADDR')) $ipaddress = getenv('REMOTE_ADDR'); else $ipaddress = 'UNKNOWN'; return $ipaddress; }