PHP 获取客户端真实IP地址多种方法小结
前端之家收集整理的这篇文章主要介绍了
PHP 获取客户端真实IP地址多种方法小结,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
经过复杂的判断与算是的获取IP地址函数
<div class="codetitle"><a style="CURSOR: pointer" data="55427" class="copybut" id="copybut55427" onclick="doCopy('code55427')"> 代码如下:
<div class="codebody" id="code55427">
function getIP() {
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}
elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR'); }
elseif (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}