Advertisement
MaxG

IP in range (PHP)

Aug 27th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.66 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * ip_in_range.php - Function to determine if an IP is located in a
  5.  *                   specific range as specified via several alternative
  6.  *                   formats.
  7.  *
  8.  * Network ranges can be specified as:
  9.  * 1. Wildcard format:     1.2.3.*
  10.  * 2. CIDR format:         1.2.3/24  OR  1.2.3.4/255.255.255.0
  11.  * 3. Start-End IP format: 1.2.3.0-1.2.3.255
  12.  *
  13.  * Return value BOOLEAN : ip_in_range($ip, $range);
  14.  *
  15.  * Copyright 2008: Paul Gregg <pgregg@pgregg.com>
  16.  * 10 January 2008
  17.  * Version: 1.2
  18.  *
  19.  * Source website: http://www.pgregg.com/projects/php/ip_in_range/
  20.  * Version 1.2
  21.  *
  22.  * This software is Donationware - if you feel you have benefited from
  23.  * the use of this tool then please consider a donation. The value of
  24.  * which is entirely left up to your discretion.
  25.  * http://www.pgregg.com/donate/
  26.  *
  27.  * Please do not remove this header, or source attibution from this file.
  28.  */
  29.  
  30.  
  31. // decbin32
  32. // In order to simplify working with IP addresses (in binary) and their
  33. // netmasks, it is easier to ensure that the binary strings are padded
  34. // with zeros out to 32 characters - IP addresses are 32 bit numbers
  35. Function decbin32 ($dec) {
  36.   return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT);
  37. }
  38.  
  39. // ip_in_range
  40. // This function takes 2 arguments, an IP address and a "range" in several
  41. // different formats.
  42. // Network ranges can be specified as:
  43. // 1. Wildcard format:     1.2.3.*
  44. // 2. CIDR format:         1.2.3/24  OR  1.2.3.4/255.255.255.0
  45. // 3. Start-End IP format: 1.2.3.0-1.2.3.255
  46. // The function will return true if the supplied IP is within the range.
  47. // Note little validation is done on the range inputs - it expects you to
  48. // use one of the above 3 formats.
  49. Function ip_in_range($ip, $range) {
  50.   if (strpos($range, '/') !== false) {
  51.     // $range is in IP/NETMASK format
  52.     list($range, $netmask) = explode('/', $range, 2);
  53.     if (strpos($netmask, '.') !== false) {
  54.       // $netmask is a 255.255.0.0 format
  55.       $netmask = str_replace('*', '0', $netmask);
  56.       $netmask_dec = ip2long($netmask);
  57.       return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
  58.     } else {
  59.       // $netmask is a CIDR size block
  60.       // fix the range argument
  61.       $x = explode('.', $range);
  62.       while(count($x)<4) $x[] = '0';
  63.       list($a,$b,$c,$d) = $x;
  64.       $range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d);
  65.       $range_dec = ip2long($range);
  66.       $ip_dec = ip2long($ip);
  67.  
  68.       # Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s
  69.      #$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0'));
  70.  
  71.       # Strategy 2 - Use math to create it
  72.      $wildcard_dec = pow(2, (32-$netmask)) - 1;
  73.       $netmask_dec = ~ $wildcard_dec;
  74.  
  75.       return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
  76.     }
  77.   } else {
  78.     // range might be 255.255.*.* or 1.2.3.0-1.2.3.255
  79.     if (strpos($range, '*') !==false) { // a.b.*.* format
  80.       // Just convert to A-B format by setting * to 0 for A and 255 for B
  81.       $lower = str_replace('*', '0', $range);
  82.       $upper = str_replace('*', '255', $range);
  83.       $range = "$lower-$upper";
  84.     }
  85.  
  86.     if (strpos($range, '-')!==false) { // A-B format
  87.       list($lower, $upper) = explode('-', $range, 2);
  88.       $lower_dec = (float)sprintf("%u",ip2long($lower));
  89.       $upper_dec = (float)sprintf("%u",ip2long($upper));
  90.       $ip_dec = (float)sprintf("%u",ip2long($ip));
  91.       return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) );
  92.     }
  93.  
  94.     echo 'Range argument is not in 1.2.3.4/24 or 1.2.3.4/255.255.255.0 format';
  95.     return false;
  96.   }
  97.  
  98. }
  99. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement