Created
December 23, 2011 09:38
-
-
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.
This file contains 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 | |
/** | |
* 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