php下获取客户端ip地址的函数
程序员文章站
2022-12-28 12:08:31
复制代码 代码如下:function getip() { $ip=getenv('remote_addr'); $ip_ = getenv('http_x_forwarde...
复制代码 代码如下:
function getip() {
$ip=getenv('remote_addr');
$ip_ = getenv('http_x_forwarded_for');
if (($ip_ != "") && ($ip_ != "unknown")) $ip=$ip_;
return $ip;
}
下面这个比较好的
复制代码 代码如下:
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;
}