PHP如何从服务器端判断客户端的代理类型。

我们假设客户端确实用了代理服务器,则PHP版的写法是:

<?php


$ra = $_SERVER["REMOTE_ADDR"];
$hv = $_SERVER["HTTP_VIA"];
$fwd = $_SERVER["HTTP_X_FORWARDED_FOR"];

/*
fwd = "";
if($_SERVER['HTTP_X_FORWARDED_FOR'])
    $fwd = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if($_SERVER['HTTP_X_FORWARDED'])
    $fwd = $_SERVER['HTTP_X_FORWARDED'];
else if($_SERVER['HTTP_FORWARDED_FOR'])
    $fwd = $_SERVER['HTTP_FORWARDED_FOR'];
else if($_SERVER['HTTP_FORWARDED'])
    $fwd = $_SERVER['HTTP_FORWARDED'];
else if($_SERVER['REMOTE_ADDR'])
    $fwd = $_SERVER['REMOTE_ADDR'];
else
    $fwd = '';
*/

// echo "HTTP_CLIENT_IP:", $_SERVER["HTTP_CLIENT_IP"], "<br />";
// echo "REMOTE_ADDR:", $ra, "<br />";
// echo "HTTP_VIA:", $hv, "<br />";
// echo "HTTP_X_FORWARDED_FOR:", $fwd, "<br />";

if(!empty($hv) || !empty($fwd))
{
    if(!empty($fwd) && $fwd != $ra)
    {
        // Transparent Proxy or Distorting Proxy
        $proxy_type = "transparent";
    }
    else
    {
        // Anonymous Proxy
        $proxy_type = "anonymous";
    }
}
else
{
    // Elite Proxy or No Proxy
    $proxy_type = "elite";
}

// Output
header("Content-type:text/json");
$dict = array("proxy_type" => $proxy_type, "REMOTE_ADDR" => "$ra", "HTTP_VIA" => "$hv", "HTTP_X_FORWARDED_FOR" => "$forward");
echo json_encode($dict);
echo "\n";
?>

在文中,有一个很长的if/else,这是加强版判断forward字段的方法,可以根据需要自行设置。如果启用了这个后,一般elite都会被判为anonymous。

Leave a Reply

Your email address will not be published.