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

前端之家收集整理的这篇文章主要介绍了php下获取客户端ip地址的函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="74792" class="copybut" id="copybut74792" onclick="doCopy('code74792')"> 代码如下:

<div class="codebody" id="code74792">
function getip() {
$IP=getenv('REMOTEADDR');
$IP
= getenv('HTTP_X_FORWARDEDFOR');
if (($IP
!= "") && ($IP != "unknown")) $IP=$IP;
return $IP;
}

下面这个比较好的
<div class="codetitle"><a style="CURSOR: pointer" data="66506" class="copybut" id="copybut66506" onclick="doCopy('code66506')"> 代码如下:
<div class="codebody" id="code66506">
function getIP()
{
if (isset($_SERVER)) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$realip = $_SERVER['HTTP_CLIENT_IP'];
} else {
$realip = $_SERVER['REMOTE_ADDR'];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")) {
$realip = getenv( "HTTP_X_FORWARDED_FOR");
} elseif (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}

原文链接:https://www.f2er.com/php/28713.html

猜你在找的PHP相关文章