php常用方法之获取客户端ip

function get_real_ip() {
    if(@$_SERVER["HTTP_ALI_CDN_REAL_IP"]){
        $ip = $_SERVER["HTTP_ALI_CDN_REAL_IP"];
    }
    elseif (@$_SERVER["HTTP_X_FORWARDED_FOR"] ?: false) {
        $ip  = $_SERVER["HTTP_X_FORWARDED_FOR"];
        $ips = explode(',', $ip);
        $ip  = $ips[0];
    } elseif (@$_SERVER["HTTP_CDN_SRC_IP"] ?: false) {
        $ip = $_SERVER["HTTP_CDN_SRC_IP"];
    } elseif (getenv('HTTP_CLIENT_IP')) {
        $ip = getenv('HTTP_CLIENT_IP');
    } elseif (getenv('HTTP_X_FORWARDED')) {
        $ip = getenv('HTTP_X_FORWARDED');
    } elseif (getenv('HTTP_FORWARDED_FOR')) {
        $ip = getenv('HTTP_FORWARDED_FOR');
    } elseif (getenv('HTTP_FORWARDED')) {
        $ip = getenv('HTTP_FORWARDED');
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    $ip = str_replace(array('::ffff:', '[', ']'), array('', '', ''), $ip);
    return $ip;
}

未经允许不得转载:任鹏个人博客 » php常用方法之获取客户端ip

赞 (0) 打赏

评论 0

取消
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏