proxyWhitelist[] = '::1'; $this->proxyWhitelist[] = '127.0.0.1'; } public function addProxiesToWhitelist($trusted_proxies) { foreach ($trusted_proxies as $proxy) { $proxy = geoip_detect_normalize_ip($proxy); if ($proxy) { $this->proxyWhitelist[] = $proxy; $this->useProxyWhitelist = true; } } } protected function getIpsFromRemoteAddr() { if (!isset($_SERVER['REMOTE_ADDR'])) return ['::1']; // REMOTE_ADDR may contain multiple adresses $ip_list = explode(',', $_SERVER['REMOTE_ADDR']); return $ip_list; } protected function getIpsFromForwardedFor($currentIpList) { $ip_list_reverse = explode(',', isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : ''); if ($this->useProxyWhitelist) { // Add the REMOTE_ADDR to the available IP pool $ip_list_reverse = array_merge($ip_list_reverse, $currentIpList); $ip_list_reverse = array_filter($ip_list_reverse, function($value) { $value = trim($value); if (!$value) return false; return false === geoip_detect_is_ip_equal($value, $this->proxyWhitelist); }); } return $ip_list_reverse; } public function getIp($useReverseProxy = false) { $ip_list = $this->getIpsFromRemoteAddr(); if ($useReverseProxy) { $ip_list = $this->getIpsFromForwardedFor($ip_list); } // Each Proxy server append their information at the end, so the last IP is most trustworthy. $ip = end($ip_list); $ip = geoip_detect_normalize_ip($ip); if (!geoip_detect_is_ip($ip)) $ip = '::1'; // By default, use localhost // this is the correct one! $ip = apply_filters('geoip_detect2_client_ip', $ip, $ip_list); return $ip; } }