$ _SERVER ['REMOTE_ADDR']返回127.0.0.1

时间:2013-10-11 05:54:48

标签: php localhost varnish

$ _ SERVER ['REMOTE_ADDR']返回127.0.0.1。

是因为我安装了清漆缓存,数据转到了清漆,然后apache和服务器认为请求来自清漆而不是客户端?

2 个答案:

答案 0 :(得分:15)

获取这样的远程IP地址:

public function getRemoteIPAddress() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        return $_SERVER['HTTP_CLIENT_IP'];

    } else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
        return $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    return $_SERVER['REMOTE_ADDR'];
}

答案 1 :(得分:3)

注意,$ _SERVER ['HTTP_X_FORWARDED_FOR']可以包含多个ips,请参阅How do I get the correct IP from HTTP_X_FORWARDED_FOR if it contains multiple IP Addresses?