Home > Backend Development > PHP Tutorial > 这段php得到用户IP的代码参数是哪来的?

这段php得到用户IP的代码参数是哪来的?

WBOY
Release: 2016-06-06 20:44:21
Original
910 people have browsed it

<code class="lang-php"><?php function real_ip()
{
    static $realip = NULL;
 
    if ($realip !== NULL)
    {
        return $realip;
    }
 
    if (isset($_SERVER))
    {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
 
            /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
            foreach ($arr AS $ip)
            {
                $ip = trim($ip);
 
                if ($ip != 'unknown')
                {
                    $realip = $ip;
 
                    break;
                }
            }
        }
        elseif (isset($_SERVER['HTTP_CLIENT_IP']))
        {
            $realip = $_SERVER['HTTP_CLIENT_IP'];
        }
        else
        {
            if (isset($_SERVER['REMOTE_ADDR']))
            {
                $realip = $_SERVER['REMOTE_ADDR'];
            }
            else
            {
                $realip = '0.0.0.0';
            }
        }
    }
    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');
        }
    }
 
    preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
    $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
 
    return $realip;
}

echo real_ip();

?>

特别是getenv里面!我在PHP手册里面就没有找到这些常量的参数啊!!!!
请问这些参数的信息是哪来的



</code>
Copy after login
Copy after login

回复内容:

<code class="lang-php"><?php function real_ip()
{
    static $realip = NULL;
 
    if ($realip !== NULL)
    {
        return $realip;
    }
 
    if (isset($_SERVER))
    {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
 
            /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
            foreach ($arr AS $ip)
            {
                $ip = trim($ip);
 
                if ($ip != 'unknown')
                {
                    $realip = $ip;
 
                    break;
                }
            }
        }
        elseif (isset($_SERVER['HTTP_CLIENT_IP']))
        {
            $realip = $_SERVER['HTTP_CLIENT_IP'];
        }
        else
        {
            if (isset($_SERVER['REMOTE_ADDR']))
            {
                $realip = $_SERVER['REMOTE_ADDR'];
            }
            else
            {
                $realip = '0.0.0.0';
            }
        }
    }
    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');
        }
    }
 
    preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
    $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
 
    return $realip;
}

echo real_ip();

?>

特别是getenv里面!我在PHP手册里面就没有找到这些常量的参数啊!!!!
请问这些参数的信息是哪来的



</code>
Copy after login
Copy after login

http://cn2.php.net/manual/zh/reserved.variables.server.php
确定没找到?另外你直接var_dump($_SERVER)不就很清楚了么。最后这篇文章有关于你这段代码的解释:http://www.cnblogs.com/lmule/archive/2010/10/15/1852020.html

如果你测试一下,估计只能得到 'REMOTE_ADDR'

但是网络环境是很复杂的,浏览器也是五花八门,上网方式也是百花齐放。

至于最上面的 'HTTP_X_FORWARDED_FOR' 很明显,这个是代理设置的。如果你翻过墙,对此应该不陌生。

此外,还有各种各样的插件啊,扩展啊之类的,也会发送请求头。

搜索引擎一搜就有很多资料了
http://www.w3school.com.cn/php/php_superglobals.asp
http://blog.csdn.net/shaobingj126/article/details/6794388
http://baike.baidu.com/view/752598.htm?fr=aladdin#3

Related labels:
php
source:php.cn
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template