Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Get content out of blocked IP addresses.
<?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;
}
<?php if( clear_from_blocked_ips() ) : ?>
<script>
alert('This content is not blocked for your IP address');
</script>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment