Skip to content

Instantly share code, notes, and snippets.

@gerasimua
Created February 3, 2015 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerasimua/a4d73b83c122770de980 to your computer and use it in GitHub Desktop.
Save gerasimua/a4d73b83c122770de980 to your computer and use it in GitHub Desktop.
Get IP with proxies
<?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