Mar 6, 2009
admin

Block IP Address using PHP Script

Hello Friends !

Do you want to block those peoples who are doing spaming ?

Once we have determined that a particular IP address is worthy of banishment, we generally invoke the

magical powers of htaccess to lock the gates. When htaccess is not available, we may summon the versatile

functionality of PHP to get the job done.

Simply edit, copy and paste the following code example into the top of any PHP for which you wish to block access:

<?php

$deny = array(“111.111.111″, “222.222.222″, “333.333.333″);

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

{

header(“location: http://www.google.com/”);

exit();
}

?>

The code basically creates an array of the IP addresses that you wish to block, and then checks incoming

addresses against the array. If the incoming (i.e., remote) address matches against any value in the array, the

function will deny access with a redirect header to the specified URL, which in this case is the majestic

Google home page. It all happens quickly, quietly, and without any fuss.

Thus, when using this code in your pages, simply replace the dummy IP addresses (i.e., “111.111.111″,

“222.222.222″, …) with those that you wish to block (e.g., “123.456.789″, “123.456.*”, “123.*”, …). Yes, PHP

understands wildcard operators (i.e., *). After editing the array of IP addresses, upload the file to your server

and relax. If you would like to verify this method, simply lookup your own IP address, add it to the array, and

try loading the target page. That’s all there is to it grab, gulp, and go.

Using this method, you may also wish to create a customized page to which blocked addresses are

redirected, perhaps to explain the situation, provide contact information, or display a macro shot of your

greasy bum. If you customize, remember to change the redirect URL (i.e., http://www.google.com/) to that

of your custom page.

If you have any problem with explanation then leave comment.

  • jericho

    Nice tip, how do you do this in reverse, say you like to block all IP except for the IP’s listed in the array?

  • jericho

    Nice tip, how do you do this in reverse, say you like to block all IP except for the IP’s listed in the array?

  • Paul

    Good question Jericho!
    I don’t see an answer though.
    I don’t think the Admin who published this knows how.

    It sure would help us if he/she responded.

  • Paul

    Good question Jericho!
    I don’t see an answer though.
    I don’t think the Admin who published this knows how.

    It sure would help us if he/she responded.

  • Paul

    One more question, how would you specify a IP range to block in the PHP code? Listing specific IPs is easy.

  • Paul

    One more question, how would you specify a IP range to block in the PHP code? Listing specific IPs is easy.

  • http://www.webforumet.no/forum/webutvikling/10480-sperre-feed.html#post95268 Sperre feed? – Webforumet.no

    [...] [...]

PHP Genious Services

I am a PHP freelancer india, PHP Developer india, PHP programmer india, Wordpress Freelancer india, Wordpress customization services, Wordpress Plugin Developer, Wordpress theme customization, Wordpress plugin customization, Magento Freelancer, Magento Developer india, Magento customization services, Magento theme integration, Opencart Developer india, Zencart Freelancer, Opencart customization, Opencart plugin development, HTML and CSS customization, cakePHP Developer, cakePHP Freelancer india, Ecommerce Developer india

Click Here for

Follow me on Facebook

Categories

Get Adobe Flash playerPlugin by wpburn.com wordpress themes