Filter IP addresses with PHP

You may at some stage want to filter an online service based on IP address. In other words, you may want to block or grant access to a request based on their IP address. This can be handled in PHP by doing the following.

If you have the IP addresses, then it is trivial.


//First check IP address is valid
$request_ip = $_SERVER['REMOTE_ADDR'];

if ( !preg_match( "/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/", $request_ip ) )
    return false;

$blacklist = array(
'111.222.12.11',
'222.111.21.22',
'221.112.11.12'
);

//check that ip is not blacklisted
if ( in_array( $request_ip, $blacklist ) )
    return false;

If you want to include a range of IP addresses, best to use a regular expression.


$blacklist_ip_range = array(
    '/^122\.244\.(\d+)\.(\d+)/', //for IP address in the range 122.244.0.0 - 122.244.255.255
    '/^123\.(\d+)\.(\d+)\.(\d+)/', //for IP address in the range 123.0.0.0 - 123.255.255.255
);

foreach( $blacklist_ip_range as $ip ) {
    if( preg_match( $ip, $request_ip ) )
       	return false;
    }

If you have a better solution, then please let me know.

JavaScript Tricks; URLEncode, Foreach and Remove last character

I have come across some neat (if not mind blowing) tricks with JavaScript recently.

Firstly, I needed JavaScript code that copied PHP’s urlencode().
This function returns an encoded string where all non-alphanumeric characters except – _ . are replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.

JavaScript has 2 functions to emulate urlencode(), but both fall short…
The escape function encodes all non-alphanumeric characters except * @ / + – _ .
The encodeURIComponent function encodes all non-alphanumeric characters except spaces and ! ‘ ( ) * ~

The winner for me though is encodeURIComponent as it encodes all UTF-8 characters while escape only encodes ISO Latin characters.

With this in hand, mozilla help provide a neat function.
Continue reading “JavaScript Tricks; URLEncode, Foreach and Remove last character”

Regular Expressions

First off, regular expressions are great.

They are a handy quick way to validate or parse data and you can use them in almost all languages. But of all things, I forget neat regex’s and in fairness they are a pita to recall as the syntax is plain nutty.

This is where it ends. I am going to reference all the neat regex’s in this blog as I come across them rather than rely on mother Google.
Continue reading “Regular Expressions”