我必须在php中获取远程系统的公共IP。 我试过了
$_SERVER['REMOTE_ADDR']
getenv('REMOTE_ADDR');
但总是返回私有IP。帮助解决它。
答案 0 :(得分:1)
试试这个:
$ip = !empty($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);
警告:您应该对其进行扩展并进行清理,因为标题可以轻松操作。
答案 1 :(得分:0)
private function getIP()
{
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
return $ip;
}
echo getIP();