Block IP addresses using .htaccess or .php file

by Prabakaran 2012-08-01 15:07:24

Block IP addresses using .htaccess or .php file

If you are looking to block some of your visitors by their IP address here are the some way that might help you.


1. Using .htaccess


If you want to block IP address using .htaccess file than just copy the below code and paste it in to your .htaccess file. You just need to change the IP address that you want to block. Replace 120.120.120.120 with the IP address that you want to block.



order deny,allow

allow from all

deny from 120.120.120.120


If you want to block series of IP address than just copy the below code and paste it in to your .htaccess file. And replace your IP address series. As per example given deny from 120.120.120 means it blocks 120.120.120.0 , 120.120.120.1 , .. , 120.120.120.255 IP addresses (means total 256 IP addresses).



order deny,allow

allow from all

deny from 120.120.120.


In some cases if you want to block bunch of IP address than just copy the below code and paste it in to your .htaccess file. And replace your IP address bunch as per yours . As per example given deny from 120.120.*. means it blocks 120.120.*.0 , 120.120.*.1 , .. , 120.120.255.255 IP addresses (means total 256 * 256 = 65536 IP addresses).



order deny,allow

allow from all

deny from 120.120.*.




2 . Using php file


If you don’t have .htaccess file access and want to block IP address using .php file than just copy the below code and paste it in to your .php file. You just need to change the IP address that you want to block. Replace 120.120.120.120 , 120.120.120.121 , 120.120.120.122 with the IP addresses that you want to block. You can define as many IP address as you want to block in an array declared below.



$block = array("120.120.120.120", "120.120.120.121", "120.120.120.122");

if (in_array ($_SERVER['REMOTE_ADDR'], $block))

{

header("location: http://www.yoursite.com/404.php?error=notallow");

exit();

}




If you want to generate .htaccess file than click here .
1071
like
0
dislike
0
mail
flag

You must LOGIN to add comments