PHP获取用户最真实IP地址

作者:LuckyMoke 时间:2017-11-21

可以避免CDN对获取用户真实IP的影响!

public function getip()
{
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], 'unknown')) {
        $realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP'] && strcasecmp($_SERVER['HTTP_CLIENT_IP'], 'unknown')) {
        $realip = $_SERVER['HTTP_CLIENT_IP'];
    } else {
        $realip = $_SERVER['REMOTE_ADDR'];
    }
    preg_match('/^((?:\d{1,3}\.){3}\d{1,3})/', $realip, $match);
    return $match ? $match[0] : false;
}

Tips

GitHubLuckyMoke

Weibokeailvyou

QQ839488083

米拓交流群

模板有不完善的地方欢迎指正!

1511438794807394.jpg