Block IP Address in PHP

02nd, January 2018 By

Some time you have get some one try to hack or spam your website and you want to block some persons from accessing your website.You will know his ip address but do not know how to prevent them from accessing your website.

You will just add this code and you will block these specific IP addresses from access your website.

$NotAllowed = array("123.123.123", "321.321.321");
$ip = $_SERVER['REMOTE_ADDR'];
if (in_array ($ip , $NotAllowed)) {

header("location: Login.php");

} else{

header("location: index.php");

}

Step1. $NotAllowed is the array of ip addresses which you want to block from accessing your website.you can add the IP addresses in this array to block.Means in place of 555.555.555 you will add your IP addresses which you will want to block.

Step2.Then using in_array() function we will check if not allowed ip address is remote address tried to login.

Step3.If yes it will redirected to the login page again if not it will goes to home page .Or it will block from access the website.

Using this simple code you will block these IP addresses to access you website,hope this will hep.

Leave a Reply

Your email address will not be published. Required fields are marked *