4

I'm using a redirect if the user's ip is between a certain IP range. However, I'm using multiple ip ranges, so I'm wondering the best way to do this. I'm current using this to redirect,

But if the IP ranges are say from 72.122.166.0-72.122.159.266 and 68.61.156.0-68.61.181.255 and 78.121.74.0-78.121.77.255 then how would I do that? Thanks!

user1875332
  • 43
  • 1
  • 5
  • 2
    I hope you aren't using this to prevent IP-banned users from accessing your page(s), because that's something pretty much useless with IP's as dynamic as people have nowadays. – quantumSoup Jul 02 '10 at 04:25
  • Also, note that `72.122.159.266` and `72.122.166.0` are not valid IP addresses, and that `68.61.181.255` and `78.121.77.255` are broadcast addresses! – fmark Jul 02 '10 at 04:52
  • see http://pgregg.com/blog/2009/04/php-algorithms-determining-if-an-ip-is-within-a-specific-range/ – Alwin Kesler Apr 11 '13 at 20:18
  • 1
    Checkout my [previous answer](http://stackoverflow.com/questions/2869893/block-specific-ip-block-from-my-website-in-php/2869931#2869931) with examples. – Marcus Adams Jul 02 '10 at 05:51

3 Answers3

19

The best way to check IP ranges is to convert the dotted address into a 32-bit number and perform comparisons on that. The ip2long function can do the conversion for you. For example:

$range_start = ip2long("68.61.156.0");
$range_end   = ip2long("68.61.181.255");
$ip          = ip2long($_SERVER['REMOTE_ADDR']);
if ($ip >= $range_start && $ip <= $range_end) {
  // blocked
}

You can put several of these ranges into an array and iterate over it to check multiple ranges.

casablanca
  • 69,683
  • 7
  • 133
  • 150
0
<?php
/* VARIABLES */
// -------------------------------
$ip = ip2long($_SERVER['REMOTE_ADDR']);
$current_ip_range = array(); // Class "X" range.

$range = (object) array();
//$range->name = '24-Bit Block';
$range->lower = ip2long('0.0.0.0'); //This is the initial value.
$range->upper = ip2long('83.50.207.254'); //This is the final value.
$current_ip_range[] = $range;
// -------------------------------

if ($ip >= $range->lower && $ip <= $range->upper) 
{
        /* ----------------------------------------
         * We ask to the server if the IP grabbed
         * is <= or => to the ip ranges and if if does
         * so, shows success message.
         * -------------------------------------- */
        echo "La IP $ip está dentro del rango";

}

else
{
        /* ----------------------------------------
         * If it isn't, shows a failure message.
         * -------------------------------------- */
        echo "La ip $ip no está dentro del rango";

}
KAZZABE
  • 197
  • 2
  • 10
  • Thnk about expanding on your answer, rather than just pasting in code (albeit with comments). – Tom Dec 28 '16 at 16:25
-1

If you're willing to use SQL, and have a table of IP ranges,

SELECT * FROM `ips` WHERE $ip BETWEEN `start` AND `end`

If you get zero results, then it's not blocked.

EDIT: Using the ip2long function, of course.

This is a better way if you have a lot of random ranges; a pure PHP way is better for fewer.

zebediah49
  • 7,467
  • 1
  • 33
  • 50