How To Block Multiple IP Addresses using PHP

In few days back tutorial, I have talked about power of .htaccess and how can you easily block hunting down and killing spammers, scrapers, and other online scum ips with the help of .htaccess, But When htaccess .htaccess is not available, we may summon the versatile functionality of PHP to get the job done.

This method is relatively straightforward. Simply copy and paste the below code example into the top of any PHP page for which you wish to block access:

if ( !file_exists('blocked_ips.txt') ) {
 $blockIPS = array(
} else {
 $blockIPS = file('blocked_ips.txt');

$ip = isset($_SERVER['REMOTE_ADDR']) ? trim($_SERVER['REMOTE_ADDR']) : '';

if ( (array_search($ip, $blockIPS))!== FALSE ) {
 echo 'Your IP adress ('.$ip.') was blocked!';

In the above code you can create .txt file and add those ips to which you want to block permanently access of your website or just simply create the array of those ips and place in above code.The function will deny access with a alert message, It all happens quickly, quietly, and without any fuss.
Thus, when using this code in your pages, simply replace the “dummy” IP addresses (i.e., “100.100.100”, “200.200.200”, …) with those that you wish to block (e.g., “123.456.789”, “123.456.*”, “123.*”, …).

If you like this post please don’t forget to subscribe my public notebook for more useful stuff

Posted in PHP