$_SERVER['REMOTE_ADDR'] 返回 127.0.0.1。
那是因为我安装了清漆缓存并且数据进入清漆,然后是apache,服务器认为请求来自清漆而不是客户端?
像这样获取远程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'];
}
请注意,$_SERVER['HTTP_X_FORWARDED_FOR'] 可以包含多个 IP,请参阅如果 HTTP_X_FORWARDED_FOR 包含多个 IP 地址,如何从 HTTP_X_FORWARDED_FOR 获取正确的 IP?