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:
block-ip-address-htaccess

if ( !file_exists('blocked_ips.txt') ) {
 $blockIPS = array(
  '127.0.0.1',
  '192.168.2.100',
  '85.78.27.90',
  '192.168.1.174'
 );
} else {
 $blockIPS = file('blocked_ips.txt');
}
// read user ip adress:
$ip = isset($_SERVER['REMOTE_ADDR']) ? trim($_SERVER['REMOTE_ADDR']) : '';
 
// search current IP in $blockIPS array
if ( (array_search($ip, $blockIPS))!== FALSE ) {
 // address is blocked:
 echo 'Your IP adress ('.$ip.') was blocked!';
 exit;
}

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