php 获得用户的真实IP 地址

180it 2021-02-28 PM 652℃ 0条

/**

  • 获得用户的真实IP 地址
    *
  • @param 多个用多行分开
  • @return void
    */

public static function get_client_ip()
{

static $realip = NULL;
if (self::$client_ip !== NULL)
{
    return self::$client_ip;
}
if (isset($_SERVER['HTTP_X_FORWARDED_FOR2']))
{
    $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR2']);
    foreach ($arr as $ip)
    {
        $ip = trim($ip);
        if ($ip != 'unknown')
        {
            $realip = $ip;
            break;
        }
    }
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
    $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    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';
    }
}
preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
self::$client_ip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
return self::$client_ip;

}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php 获得用户的真实IP 地址