Block IP address with PHP
- Friday, December 26, 2008, 6:54
- PHP & MySQL
- 8 comments
Its always a hard slog with spammers around the web. Once we have found the culprit, we generally move towards htaccess to lock the window and when its not available, we rely on the power of PHP.Lets get into the battle…
<?php
$denyIP = array(“IP Address 1″, “IP Address 2″, “IP Address 3″);
if (in_array ($_SERVER['REMOTE_ADDR'], $denyIP)) {
header(“location: http://www.microsoft.com/”);
exit();
} ?>
The above code creates an array of IP address which needs to be blocked and it checks the incoming address with array. What will happen when the IP address mataches with any value in the array?
It will simply redirect to the address below, ie [ http://www.microsoft.com ]
You can setup a redirect page, instead of Microsoft.com where you can explain the visitor to contact you or tell him why the site is blocked.
Popularity: 64% [?]
About the Author
8 Comments on “Block IP address with PHP”
Write a Comment
Gravatars are small images that can show your personality. You can get your gravatar for free today!
very useful info
thanks
would be very handy if it offered a range of from and to (inclusive).
Can this be done and how?
Many thanks
Porl
Porl…
What type of range would you like to block… now it may come in handy at rare occation.
IP’s for countries are given in a group of ranges, that would therefore be something like:
215.222.777.0 to 215.222.999.0
and anything in that range is banned (inclusive of start and end figure.
the ability to do this large scale for social sites etc to block out africa etc…
and also singly as well. could be a text file or a mysql database.
Hi, porl…. so you can possibly look into this code below…
What about blocking certain domains instead of IP or IP ranges? This would be very helpful to me. I appreciate any help.
Thanks
Mateo
Why dont you use .htaccess to block a domain. Its much better practice.
Wolf gives excellent advice and I know that you will all love reading what he has to say to you! ,