帮助中心

当前位置:首页 > 帮助中心 > 主机相关

使用了CDN后取真实IP的PHP代码

取真实IP的php代码
(discuz修改include/common.inc.php)
用以下这段代码:
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
        $onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 

'unknown')) {
        //使用CDN后判断真实ip by CDNUNION Johnny
        
        $testip = explode('.', getenv('HTTP_X_FORWARDED_FOR'));

        if ($testip[0]=='192' && $testip[1]=='168') {
                $onlineip = getenv('REMOTE_ADDR');
        }
    elseif($testip[0]=='10') {
                $onlineip = getenv('REMOTE_ADDR');
        }
        else {
                $onlineip = getenv('HTTP_X_FORWARDED_FOR');
        }

        //gamesir hack end} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 

'unknown')) {
        $onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER

['REMOTE_ADDR'], 'unknown')) {
        //by Johnny
 $tmp_ip = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
 $tmp_ip1 = explode(',',$tmp_ip[0]);
 if ($tmp_ip1[0] == '192' && $tmp_ip1[1] == '168') {
  $onlineip = getenv('REMOTE_ADDR');
 }else if($tmp_ip1[0]=='10') {
                $onlineip = getenv('REMOTE_ADDR');
        }
        else{
                $onlineip = $tmp_ip[0];
        }
 unset($tmp_ip);unset($tmp_ip1);
 
}

替换这段代码:
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
 $onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 

'unknown')) {
 $onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
 $onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER

['REMOTE_ADDR'], 'unknown')) {
 $onlineip = $_SERVER['REMOTE_ADDR'];
}
阅读次数: 7869发布日期:2009-6-16返回