Block IP Address With PHP

Today’s PHP trick will allow us to do something that a site administrator would usually do whenever a member misbehaves in the forum (e.g. violates site rules or Internet etiquette): prevent the user from visiting the site by blocking their IP address.

php Block IP Address With PHP

This trick was shared by Monu Alagh over at TricksForYou and it involves creating two .php files. The steps involve accomplishing this is as follows:

Using Notepad or any source code editor such as Notepad++, key in this code:

<?php
$ip_address = $_SERVER(‘REMOTE_ADDR);
$ip_blocked = array(‘127.0.0.1’, ‘100.100.100.100’);
?>
  • Save it as confic.inc.php and put it in your www folder where your server is installed.
  • Create another .php file and name it index2.php
  • Key in the following code below:
<?php
Require (con fic.inc.php);
Foreach($ip_address as $ip){
if($ip==$ip_address){
die(‘your ip address ‘.$ip_address.’ has been blocked);
}
}
?>
  • Save it in the same folder as confic.inc.php

And that’s all there is to it!

Incoming search terms for the article:

Related Posts

How to Censor Words Using PHP

Add A Full Image Background To Your WordPress Login Screen

Single Page App Book

Non-Recursively Deleting A Folder In PHP

Leave a Reply









CommentLuv Enabled