Created
February 3, 2015 11:37
-
-
Save gerasimua/a4d73b83c122770de980 to your computer and use it in GitHub Desktop.
Get IP with proxies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function getIp() | |
{ | |
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { | |
$proxies = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); | |
$proxies = array_reverse($proxies); | |
foreach ($proxies as $proxy) { | |
if ($this->isPrivateIp(trim($proxy))) { | |
return $this->isPrivateIp(trim($proxy)); | |
} | |
} | |
} | |
if (isset($_SERVER['HTTP_X_REAL_IP'])) { | |
return $_SERVER['HTTP_X_REAL_IP']; | |
} | |
return $_SERVER['REMOTE_ADDR']; | |
} | |
protected function isPrivateIp($ip) | |
{ | |
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment