Block IP addresses using .htaccess or .php file
Hello Friends ,
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.
<?php
$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 .
Cheers !!
Related posts:
I am
I am Shail Patel studing in MCA and working as PHP developer(Part time) in ahmedabad. I was looking forward to share my internet ideas to people of this beautiful world. I found this site as an ideal one to do this. Now i am author of this site and i hope my articles will be helpful to you guys.
about 11 months ago
thanks for your information, i’ll try it in my blog
about 9 months ago
very good info
thank you
jocuri
about 7 months ago
In 2001 Terry Semel then took a CEO position at the Search Engine and Media giant Yahoo.;
about 7 months ago
The good news is that these issues can be avoided by choosing the right insurance for tourist policy.;
about 4 months ago
nice one. helped me lot