Skip to content

Instantly share code, notes, and snippets.

@webtechriser
Created February 11, 2020 06:55
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 webtechriser/b958d710d50956682331221a7b09c994 to your computer and use it in GitHub Desktop.
Save webtechriser/b958d710d50956682331221a7b09c994 to your computer and use it in GitHub Desktop.
Get Public IP Address
<?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