我想获取使用我网站的客户端IP地址。我使用PHP $_SERVER超全局变量:
$_SERVER['REMOTE_ADDR'];
但是我看到它不能给出正确的IP地址使用这个。我得到我的IP地址,并看到它与我的IP地址不同,我也可以在一些网站上看到我的IP地址,如:
http://whatismyipaddress.com/
我粘贴的IP地址,给我的PHP功能,但这个网站显示没有结果。这个问题是怎么来的,我怎么能得到客户端的IP地址?
我想获取使用我网站的客户端IP地址。我使用PHP $_SERVER超全局变量:
$_SERVER['REMOTE_ADDR'];
但是我看到它不能给出正确的IP地址使用这个。我得到我的IP地址,并看到它与我的IP地址不同,我也可以在一些网站上看到我的IP地址,如:
http://whatismyipaddress.com/
我粘贴的IP地址,给我的PHP功能,但这个网站显示没有结果。这个问题是怎么来的,我怎么能得到客户端的IP地址?
获取访问者/客户端的IP地址最简单的方法是使用$_SERVER['REMOTE_ADDR']或$_SERVER['REMOTE_HOST']变量。
但是,有时这并不能返回访问者的正确IP地址,因此我们可以使用其他一些服务器变量来获取IP地址。
下面两个函数是等价的,区别只在于如何检索值以及从哪里检索值。
getenv()用于在PHP中获取环境变量的值。
// 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服务器创建的服务器变量的数组。
// 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;
}
下面是一个使用本地和局域网IP地址过滤器获取IP地址的函数:
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;
}
}
}
}
}
在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';