how to check IP on the basis of IP Range?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robin1983
    New Member
    • Oct 2007
    • 99

    how to check IP on the basis of IP Range?

    Hi All,

    I need a help from you guys. I have two tables once containing some list of ip range and other tables which contain the login id and their respective ip address from where they have logged in.

    Now, I want to run a report on the daily basis to check whether the ips in the second table is come to ip range in the first table.

    The table architecture is as follows:
    iprange
    id fromIP endIP
    1 123.11.11.34 123.11.13.45
    2 123.11.11.54 123.11.13.67
    3 123.11.15.34 123.11.18.46 etc

    now I have other table
    logintable
    ID username logip
    1 A1 123.11.11.33
    2 a3 123.15.11.34

    the issue, i want to create php file where it checks the ip in logintable to iprange. If the record found, then show the details from logintable for the particular order.

    Thank in advance for your help.

    With Regards,
    Robin
    New Delhi - India
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Have a look at ip2long().
    And then I imagine something like: (I have not done this before)
    Code:
    function checkIP($user_ip, $lower_ip, $upper_ip) {
    $lower = ip2long($lower_ip);
    $upper = ip2long($upper_ip);
    $user = ip2long($user_ip);
    return ( ($user>=$lower) && ($user<=$upper) );
    }

    Comment

    • robin1983
      New Member
      • Oct 2007
      • 99

      #3
      Originally posted by TheServant
      Have a look at ip2long().
      And then I imagine something like: (I have not done this before)
      Code:
      function checkIP($user_ip, $lower_ip, $upper_ip) {
      $lower = ip2long($lower_ip);
      $upper = ip2long($upper_ip);
      $user = ip2long($user_ip);
      return ( ($user>=$lower) && ($user<=$upper) );
      }
      Thanks for the nice response. Also, I was searching more about on the site I found a tutorial http://pgregg.com/blog/2009/04/php-a...fic-range.html which shows to check the ips. According to the code, i just modify few still I am not able to get the correct result.

      The following are the code that I used

      Code:
      <?php
      
      	include_once "db_connect.inc.php";
      	include_once "ip_in_range.php";
       // query to fetch the ip from my table logip2010_07_02 which have around 3000 records
       $query = mysql_query("select * from logip2010_07_02 ") or die(mysql_error());
        while($ip = mysql_fetch_array($query)){
        $ip = $ip['loginIP'];
       //echo $ip."<br>";
        $iprange = mysql_query("Select * from nigeriaiprange limit 0,10") or die(mysql_error());
        while($ipr = mysql_fetch_array($iprange)){
        $range = '"'.$ipr['fromIP'].'-'.$ipr['toIP'].'"<br>';
       //echo $range;
        //$range = "10.134.21.1-10.134.24.1";
        $ok = ip_in_range($ip, $range);
        echo $ip, ($ok ? ' OK' : ' Fail'), "<br>";
        }
        }
      ?>
      the following is ip_in_range() function code that I got from the tutorial. 
      
      <?php 
      function decbin32 ($dec) {
        return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT);
      }
      Function ip_in_range($ip, $range) {
        if (strpos($range, '/') !== false) {
          // $range is in IP/NETMASK format
          list($range, $netmask) = explode('/', $range, 2);
          if (strpos($netmask, '.') !== false) {
            // $netmask is a 255.255.0.0 format
            $netmask = str_replace('*', '0', $netmask);
            $netmask_dec = ip2long($netmask);
            return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
          } else {
            // $netmask is a CIDR size block
            // fix the range argument
            $x = explode('.', $range);
            while(count($x)<4) $x[] = '0';
            list($a,$b,$c,$d) = $x;
            $range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d);
            $range_dec = ip2long($range);
            $ip_dec = ip2long($ip);
      
            # Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s
            #$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0'));
      
            # Strategy 2 - Use math to create it
            $wildcard_dec = pow(2, (32-$netmask)) - 1;
            $netmask_dec = ~ $wildcard_dec;
      
            return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
          }
        } else {
          // range might be 255.255.*.* or 1.2.3.0-1.2.3.255
          if (strpos($range, '*') !==false) { // a.b.*.* format
            // Just convert to A-B format by setting * to 0 for A and 255 for B
            $lower = str_replace('*', '0', $range);
            $upper = str_replace('*', '255', $range);
            $range = "$lower-$upper";
          }
      
          if (strpos($range, '-')!==false) { // A-B format
            list($lower, $upper) = explode('-', $range, 2);
            $lower_dec = (float)sprintf("%u",ip2long($lower));
            $upper_dec = (float)sprintf("%u",ip2long($upper));
            $ip_dec = (float)sprintf("%u",ip2long($ip));
            return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) );
          }
      
          echo 'Range argument is not in 1.2.3.4/24 or 1.2.3.4/255.255.255.0 format';
          return false;
        }
      
      }
      ?>
      Even after the ip are present in the range it shows NO instead of OK.

      Kindly check and help me in resolving the issue.

      Regards,
      Robin

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        That code is a lot more involved than mine and is for special statements like: 195.25.*.* etc instead of the range as you originally requested. I am also confused as the result should be either FAIL or OK, instead of NO as you said it does. Can you confirm that?

        I didn't write their code, and I imagine it's working. If not maybe you can contact the author, but it looks fine, and without more information, I can't tell what the problem is. How are you calling the function and your IP? It may be that your input format is wrong.

        Comment

        Working...