PHP: 使用 lighttpd 获取用户的 IP 地址
PHP: getting user's IP address using lighttpd
我试过这个功能,但这总是returnhost/website的IP地址而不是用户的IP
知道为什么以及如何解决它吗?
该网站未托管在本地主机上,我正在使用 lighttpd 托管该网站
<?php
function getUserIpAddr(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$ip = getUserIpAddr();
echo $ip;
?>
<?php
// Print IP of user
$ip = $_SERVER['REMOTE_ADDR'];
$ip = htmlentities($ip, ENT_COMPAT, "UTF-8");
echo"$ip";
// You can also get the users hostname
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$hostname = htmlentities($hostname, ENT_COMPAT, "UTF-8");
echo"$hostname";
?>
试试这个代码
$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_X_CLUSTER_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
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';
echo $ipaddress;
lighttpd在CGI环境变量REMOTE_ADDR
.
中提供远程地址
lighttpd 不会设置 HTTP_CLIENT_IP
除非请求提供 Client-IP
header。 lighttpd 不会设置 HTTP_X_FORWARDED_FOR
除非请求提供 X-Forwarded-For
header.
在您使用的 header 中,只有 REMOTE_ADDR
是可信的。您永远不应该信任客户端控制的数据。如果您的设置有设置 header 的中间代理,例如 X-Forwarded-For
,并且您信任中间代理,并且除了受信任的代理之外,其他任何东西都无法访问您的服务器,那么那些 header s也值得信赖
如果您的设置是在受信任的代理后面并且您想使用 X-Forwarded-For
,请参阅 lighttpd mod_extforward,它可以解析 X-Forwarded-For
或标准化的 header 等as Forwarded
,把上游远程地址给你写成REMOTE_ADDR
。 lighttpd mod_extforward 还支持 HAProxy PROXY
协议。
我试过这个功能,但这总是returnhost/website的IP地址而不是用户的IP 知道为什么以及如何解决它吗?
该网站未托管在本地主机上,我正在使用 lighttpd 托管该网站
<?php
function getUserIpAddr(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$ip = getUserIpAddr();
echo $ip;
?>
<?php
// Print IP of user
$ip = $_SERVER['REMOTE_ADDR'];
$ip = htmlentities($ip, ENT_COMPAT, "UTF-8");
echo"$ip";
// You can also get the users hostname
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$hostname = htmlentities($hostname, ENT_COMPAT, "UTF-8");
echo"$hostname";
?>
试试这个代码
$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_X_CLUSTER_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
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';
echo $ipaddress;
lighttpd在CGI环境变量REMOTE_ADDR
.
lighttpd 不会设置 HTTP_CLIENT_IP
除非请求提供 Client-IP
header。 lighttpd 不会设置 HTTP_X_FORWARDED_FOR
除非请求提供 X-Forwarded-For
header.
在您使用的 header 中,只有 REMOTE_ADDR
是可信的。您永远不应该信任客户端控制的数据。如果您的设置有设置 header 的中间代理,例如 X-Forwarded-For
,并且您信任中间代理,并且除了受信任的代理之外,其他任何东西都无法访问您的服务器,那么那些 header s也值得信赖
如果您的设置是在受信任的代理后面并且您想使用 X-Forwarded-For
,请参阅 lighttpd mod_extforward,它可以解析 X-Forwarded-For
或标准化的 header 等as Forwarded
,把上游远程地址给你写成REMOTE_ADDR
。 lighttpd mod_extforward 还支持 HAProxy PROXY
协议。