Block IP address with PHP

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

Vinothbabu has written 24 stories on this site.

In short, my name is Vinoth babu { perra kettala summa authrithala } a UI expert from India, Chennai.

8 Comments on “Block IP address with PHP”

  • Honour Chick wrote on 28 December, 2008, 1:05

    very useful info :) thanks

  • Porl wrote on 7 January, 2009, 0:08

    would be very handy if it offered a range of from and to (inclusive).
    Can this be done and how?

    Many thanks

    Porl

  • Vinothbabu wrote on 8 January, 2009, 10:19

    Porl…

    What type of range would you like to block… now it may come in handy at rare occation.

  • porl wrote on 9 January, 2009, 6:37

    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.

  • Vinothbabu wrote on 13 January, 2009, 11:19

    Hi, porl…. so you can possibly look into this code below…

    < ?php
    function ipCheck($match_ip, $ip_array) {
    We have to make sure that its an array, before we go with foreach.
    if (is_array($ip_array)) {
    // loop through ip array
    foreach ($ip_array as $ip_add) {
    // we are testing if there is a match
    if (strpos($match_ip, $ip_add)===0) {
    return true;
    }
    }
    }
    return false;
    }
    $ban_array = array(
    '216.47.55.141',
    '72.47.147',
    '74.171',
    '58'
    );
    if (ipCheck($_SERVER['REMOTE_ADDR'], $ban_array)) {
    echo "Your message here.";
    exit;
    }
    ?>

  • mateo wrote on 26 January, 2009, 19:53

    What about blocking certain domains instead of IP or IP ranges? This would be very helpful to me. I appreciate any help.

    Thanks
    Mateo

  • Vinothbabu wrote on 29 January, 2009, 11:28

    Why dont you use .htaccess to block a domain. Its much better practice.

  • Merlin59 wrote on 23 October, 2009, 2:08

    Wolf gives excellent advice and I know that you will all love reading what he has to say to you! ,

Write a Comment

Gravatars are small images that can show your personality. You can get your gravatar for free today!

Copyright © 2010 HTTPguru. All rights reserved.

Our Partners:
WebHosting Jobs| UI expert | Packet Sniffer  | Mobile Home Refinancing  | Laptop LCD screens | CSS Creator| Exchange Hosting| Compare cable companies| Usa online casino | vps | Coupon Codes