Rumah > php教程 > php手册 > php获取客户端ip地址

php获取客户端ip地址

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Lepaskan: 2016-06-02 09:14:29
asal
1014 orang telah melayarinya

本教程提供几款获取IP地址的代码,各有各的优点,这里获取用户ip 是利用php的全局变量实现的,下面来看看有没有你要找的,php实例代码如下:

//最简单的方法,代码如下: 

<?php
$ip = $_server["remote_addr"];
echo $ip;
//最实用获取用户ip地址代码,代码如下:
function get_real_ip() {
    $ip = false;
    if (!emptyempty($_server["http_client_ip"])) {
        $ip = $_server["http_client_ip"];
    }
    if (!emptyempty($_server[&#39;http_x_forwarded_for&#39;])) {
        $ips = explode(", ", $_server[&#39;http_x_forwarded_for&#39;]);
        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[&#39;remote_addr&#39;]);
}
echo get_real_ip();
//获取ip地址与端口号,代码如下:
$ip = $_server["remote_addr"];
$port = $_server[&#39;remote_port&#39;];
echo $ip . $port;
//有一点意思
if (getenv(&#39;http_client_ip&#39;)) {
    $onlineip = getenv(&#39;http_client_ip&#39;);
} elseif (getenv(&#39;http_x_forwarded_for&#39;)) {
    $onlineip = getenv(&#39;http_x_forwarded_for&#39;);
} elseif (getenv(&#39;remote_addr&#39;)) {
    $onlineip = getenv(&#39;remote_addr&#39;);
} else {
    $onlineip = $http_server_vars[&#39;remote_addr&#39;];
}
echo $onlineip;
echo " 
";
//利用qq接口,代码如下:
function get_ip_place() {
    $ip = file_get_contents("http://fw.qq.com/ipaddress");
    $ip = str_replace(&#39;"&#39;, &#39; &#39;, $ip);
    $ip2 = explode("(", $ip);
    $a = substr($ip2[1], 0, -2);
    $b = explode(",", $a);
    return $b;
}
$ip = get_ip_place();
print_r($ip);
?>
Salin selepas log masuk


本文地址:

转载随意,但请附上文章地址:-)

Label berkaitan:
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan