PHP visitorIP()返回localhost地址

时间:2017-04-24 13:59:05

标签: php apache nginx geolocation ip-address

我使用下面的php代码获取用户的IP地址以设置其位置。它适用于我们的QA环境,但在我们的prod环境中,它始终将IP返回为127.0.0.1。我想在两个环境之间必须有一些不同的Apache / Nginx(我们同时使用)配置,但我不知道哪个配置会对此产生影响(?)。这两种环境之间的唯一区别就是在我们的产品环境中(托管在RackSpace云中),流量首先会达到负载均衡器......但我不明白为什么这会将IP改为localhost所以我不相信这种情况 - 如果我错了,请纠正我。

<?php 

$userIp = visitorIP();
$ipquad = explode(".", $userIp);
$ipnum = 16777216 * $ipquad[0] + 65536 * $ipquad[1] + 256 * $ipquad[2] + $ipquad[3];
var_dump('IP:- '.$userIp);
var_dump($ipquad);
var_dump('ipnum:- '.$ipnum);
die;

function visitorIP() {

    $ip = $_SERVER['REMOTE_ADDR'];

    if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && urlIsPublic($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } 

    /* Check for Akamai custom header */
    if(isset($_SERVER['HTTP_TRUE_CLIENT_IP'])){
        $ip = $_SERVER['HTTP_TRUE_CLIENT_IP'];
    }   


    if (!(urlIsPublic(trim($ip)))) {
        if(defined('IP')) {
            return IP;
        }
    }

    return trim($ip);
}

function urlIsPublic($ip) {
    if(filter_var($ip,
                           FILTER_VALIDATE_IP,
                           FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)
                !== false) {
        return true;
    }
    return false;

} 

?>

var_dump($ _ SERVER)的输出低于......

array(29) { ["APPLICATION_ENV"]=> string(10) "production" ["HTTP_HOST"]=> string(19) "www.example.com" ["HTTP_X_FORWARDED_FOR"]=> string(32) "<MY_PUBLIC_IP>, 192.168.200.154" ["HTTP_CONNECTION"]=> string(5) "close" ["HTTP_CACHE_CONTROL"]=> string(9) "max-age=0" ["HTTP_UPGRADE_INSECURE_REQUESTS"]=> string(1) "1" ["HTTP_USER_AGENT"]=> string(109) "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36" ["HTTP_ACCEPT"]=> string(74) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" ["HTTP_ACCEPT_ENCODING"]=> string(19) "gzip, deflate, sdch" ["HTTP_ACCEPT_LANGUAGE"]=> string(26) "en-GB,en-US;q=0.8,en;q=0.6" ["PATH"]=> string(29) "/sbin:/usr/sbin:/bin:/usr/bin" ["SERVER_SIGNATURE"]=> string(80) "
Apache/2.2.15 (CentOS) Server at www.example.com Port 80
" ["SERVER_SOFTWARE"]=> string(22) "Apache/2.2.15 (CentOS)" ["SERVER_NAME"]=> string(19) "www.example.com" ["SERVER_ADDR"]=> string(9) "127.0.0.1" ["SERVER_PORT"]=> string(2) "80" ["REMOTE_ADDR"]=> string(9) "127.0.0.1" ["DOCUMENT_ROOT"]=> string(37) "/var/www/vhosts/example/httpdocs" ["SERVER_ADMIN"]=> string(14) "root@localhost" ["SCRIPT_FILENAME"]=> string(48) "/var/www/vhosts/example/httpdocs/ipinfo.php" ["REMOTE_PORT"]=> string(5) "45532" ["GATEWAY_INTERFACE"]=> string(7) "CGI/1.1" ["SERVER_PROTOCOL"]=> string(8) "HTTP/1.0" ["REQUEST_METHOD"]=> string(3) "GET" ["QUERY_STRING"]=> string(0) "" ["REQUEST_URI"]=> string(11) "/ipinfo.php" ["SCRIPT_NAME"]=> string(11) "/ipinfo.php" ["PHP_SELF"]=> string(11) "/ipinfo.php" ["REQUEST_TIME"]=> int(1493132187) }

1 个答案:

答案 0 :(得分:1)

由于它是以逗号分隔的列表,因此您需要检查每个IP:

$ip

然后您可以像这样使用它,并在稍后检查if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = urlIsPublic($_SERVER['HTTP_X_FORWARDED_FOR']); } 是否为false:

@TotalHours