调试各种 php 取cdn客户端ip cloudflare 等

<?php
echo"<br>HTTP_CF-Connecting-IP:".$_SERVER['HTTP_CF_CONNECTING_IP'];  //取 cloudflare.com cdnip
echo"<br>X-Forwarded-For:".$_SERVER['X-Forwarded-For'];  //取通用cdn
echo"<br>X-Real-IP:".$_SERVER['HTTP_X_REAL_IP'];  //取cdn反向代理ip
echo"<br>HTTP_ALI_CDN_REAL_IP:".$_SERVER['HTTP_ALI_CDN_REAL_IP']; // 取其他cdn返回ip
echo"<br>HTTP_X_FORWARDED_FOR:".$_SERVER['HTTP_X_FORWARDED_FOR '];  //获取代理服务器转发的客户端IP地址。如果使用代理服务器进行访问,则客户端真实的IP地址可能被代理服务器伪造。
echo"<br>HTTP_CLIENT_IP:".$_SERVER['HTTP_CLIENT_IP'];//获取客户端IP地址。与HTTP_X_FORWARDED_FOR类似,也需要防止被伪造
echo"<br>HTTP_PROXY_USER:".$_SERVER['HTTP_PROXY_USER'];
echo"<br>REMOTE_ADDR:".$_SERVER['REMOTE_ADDR']; //获取访问客户端的IP地址
echo"<br>REQUEST_URI:".$_SERVER['REQUEST_URI']; //取url文件名加参数
echo"<br>SERVER_ADDR:".$_SERVER['SERVER_ADDR']; //取本地ip
echo"<br>方法ip".mac_get_client_ip();

function mac_get_client_ip()
{
    static $final;
    if (!is_null($final)) {
        return $final;
    }
    $ips = [];
    $real_ip = getenv('HTTP_X_REAL_IP'); 
    $cf_ip = $_SERVER['HTTP_CF_CONNECTING_IP']; 
    $ali_ip = $_SERVER['HTTP_ALI_CDN_REAL_IP']; 
    $real1_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    $x_ip = $_SERVER['X-Forwarded-For'];
    $cli_ip = $_SERVER['HTTP_CLIENT_IP']; 
    $prous_ip = $_SERVER['HTTP_PROXY_USER']; 
    $rem_ip = $_SERVER['REMOTE_ADDR'];
    $srad_ip = $_SERVER['SERVER_ADDR'];
    if (!empty($cf_ip)) {
        $ips[] = $cf_ip;
    }
    else  if (!empty($ali_ip)) {
        $ips[] = $ali_ip;
    }
    else  if (!empty($real1_ip)) {
        $ips[] = $real1_ip;//取nginx反向代理 客户ip
    }
        else  if (!empty($x_ip)) {
        $ips[] = $x_ip;//取nginx反向代理 客户ip
    }
    else if (!empty($cli_ip)) {
        $ips[] = $cli_ip; //可能是用户真实IP也可能是代理IP
    }
   else if (!empty($prous_ip)) {
        $ips[] = $prous_ip;
    }
   
   else if (!empty($real_ip)) {
        $ips[] = $real_ip; 
    }
      else if (!empty($srad_ip)) {
        $ips[] =$srad_ip;
    }
    else if (!empty($rem_ip)) {
        $ips[] =$rem_ip;
    }
 
    else
    {   
        $ips[] ='0.0.0.0';
    }
    // 选第一个最合法的,或最后一个正常的IP
    foreach ($ips as $ip) {
        $verifyResult = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE);
        if (!$verifyResult){
            continue;
        }
        $verifyResult && $final = $ip;
    }
    empty($final) && $final = '0.0.0.0';
    return $final;
}
?>
© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容