Get Public IP Address
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 Public IP Address | |
* Get the Public IP address of the client. | |
* | |
* @link http://stackoverflow.com/a/2031935/1743124 | |
* | |
* @return string The IP Address. | |
*/ | |
function get_public_ip_address(){ | |
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){ | |
if (array_key_exists($key, $_SERVER) === true){ | |
foreach (explode(',', $_SERVER[$key]) as $ip){ | |
$ip = trim($ip); // just to be safe | |
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){ | |
return $ip; | |
} | |
} | |
} | |
} | |
} | |
/** | |
* IP Clearance | |
* Make a clearance for IP addresses. | |
* | |
* @return boolean Clearance true, if not within the range. | |
*/ | |
function clear_from_blocked_ips() { | |
$ip_address = get_public_ip_address(); | |
$blocked_ips = array( | |
'337.537.112.181', | |
'258.10.520.12' | |
); | |
if( in_array( $ip_address, $blocked_ips ) ) | |
return false; | |
else | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment