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;
}

最简单获取ip地址代码一句实例
<div class="codetitle"><a style="CURSOR: pointer" data="47141" class="copybut" id="copybut47141" onclick="doCopy('code47141')"> 代码如下:
<div class="codebody" id="code47141">
$reIP=$_SERVER["REMOTE_ADDR"];
echo $reIP;

PHP获取ip的算法
<div class="codetitle"><a style="CURSOR: pointer" data="25114" class="copybut" id="copybut25114" onclick="doCopy('code25114')"> 代码如下:
<div class="codebody" id="code25114">
if(getenv('HTTP_CLIENT_IP')) {
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR')) {
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR')) {
$onlineip = getenv('REMOTE_ADDR');
} else {
$onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
}
echo $onlineip;

可以分出内网与外网站ip地址获取程序
<div class="codetitle"><a style="CURSOR: pointer" data="64635" class="copybut" id="copybut64635" onclick="doCopy('code64635')"> 代码如下:
<div class="codebody" id="code64635">
function getip_out(){
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips教程 = explode (",",$_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip) { array_unshift($ips,$ip); $ip = FALSE; }
for ($i = 0; $i < count($ips); $i++) {
if (!eregi ("^(10│172.16│192.168).",$ips[$i])) {
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
echo getip_out();

PHP获取ip的算法,用了?号表达式来处理
<div class="codetitle"><a style="CURSOR: pointer" data="46073" class="copybut" id="copybut46073" onclick="doCopy('code46073')"> 代码如下:<div class="codebody" id="code46073">
$user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
$user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];

php下获取客户端ip地址的函数
php 获取客户端的真实ip

原文链接:https://www.f2er.com/php/28567.html
PHP真实IP

猜你在找的PHP相关文章