Skip to content

Instantly share code, notes, and snippets.

@itavero
Created December 23, 2011 09:38
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 itavero/1513728 to your computer and use it in GitHub Desktop.
Save itavero/1513728 to your computer and use it in GitHub Desktop.
Get the real IP address of the client and optionally also the one from the proxy server.
<?php
/**
* Get the IP address of the client.
*
* @author Arno Moonen <info@arnom.nl>
* @version 0.1-201112231035
* @param boolean $get_all Set to true to get an array containing all the IP addresses found.
* @return mixed Depending on the value of $get_all the return value will be either a string or an array
*/
function am_get_real_ip($get_all = false)
{
$keys = array(
"HTTP_CLIENT_IP",
"HTTP_X_FORWARDED_FOR",
"HTTP_X_FORWARDED",
"HTTP_FORWARDED_FOR",
"HTTP_FORWARDED",
"REMOTE_ADDR",
);
$ips = array();
foreach ($keys as $X) {
if (array_key_exists($X, $_SERVER) && !empty($_SERVER[$X])) {
if (!$get_all) {
return $_SERVER[$X];
}
$ips[$X] = $_SERVER[$X];
}
}
return array_unique($ips, SORT_STRING);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment