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

php获取客户端ip地址

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-02 09:14:29
Original
1011 people have browsed it

本教程提供几款获取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);
?>
Copy after login


本文地址:

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

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template