hi
i have a list of IP from my database and i would to check if the visitor's IP is in that list.

This one works:

if (preg_match('/^' . $ipfromdb . '$/', $_SERVER['REMOTE_ADDR'])) { 

but if I've defined a range (eg 192.168.2.1-20) the preg_match above stop to work.

How can I add the - symbol to validate a range?

thanks

    $iprange - "192.168.2.[1-2]{1}[0-9]{1}";

    but I have to ask why not search the db for the ip... SELECT column FROM ip_table WHERE ip='$_SERVER['REMOTE_ADDR']' ?

      I can't search the db for the ip because some records are as
      192.168.1.2-50
      209.85.148.106-110
      and so on.

      How can I apply your code into my preg_match in order to check if the visitor's ip is (for example) between 192.168.1.2 and 192.168.1.50?

        Well I would fix the DB to not have ranges - if that's not an option then you could search for a dash in the value, then work from there. I am completely bored so below you will see my solution. I'm not using preg_match because the original post the range began with 1 but the others do not.

        <?php
        
        // TESTS
        $ranges = array('192.168.1.2-50','209.85.148.106-110');
        $testips = array('192.168.1.32','209.85.148.108','201.27.32.48');
        
        foreach( $testips as $tip ) {
           foreach( $ranges as $range ) {
              if( ipInRange($tip,$range) ) {
                 echo $tip .' found in '. $range .'<br>';
              } else {
                 echo $tip .' not in '. $range .'<br>';
              }
           }
        }
        
        function ipInRange($ip,$range) {
           $r_parts = explode('.',$range);
           list($start,$end) = explode('-',$r_parts[3]);
           unset($r_parts[3]);
           for( $i=$start; $i<=$end; $i++ ) {
              $tip = implode('.',$r_parts) . '.' . $i;
              if( $tip == $ip ) return TRUE;
           }
           return FALSE;
        }

          I'd also probably prefer to to separate minimum and maximum IP addresses into separate columns. Also, note the existence in PHP of [man]ip2long[/man].

            I agree with splitting ranges into min and max ip so you can use ip BETWEEN min AND max. However, can't you use pattern matching directly against your database?

              johanafm;10992587 wrote:

              I agree with splitting ranges into min and max ip so you can use ip BETWEEN min AND max. However, can't you use pattern matching directly against your database?

              I revisited my table. Now I can to have the IP as:

              192.168.2.*
              192.168.1.1

              Can you post an example to use pattern matching directly against in db table? thanks

                I'd like to once again point out that it's better to use min and max ip, each in separate columns, and CREATE INDEX (ipmin, ipmax).

                If you don't have a range when you insert allowed addresses, insert the same value in both columns since x BETWEEN x AND x => true, for all x.

                Thus, your .* would be inserted as the separate values 192.168.2.1 and 192.168.2.254. Unless you go with weedpackets suggestion and use ip2long, which requires less space. They index key will also be smaller and search speed should be improved. The downside obviously is that you won't directly see what ip addresses you have, but depending onf your DBMS, you might have functions that does the same as PHP's ip2long and long2ip, such as INET_ATON() and INET_NTOA()

                As for pattern matching, it depends on what database you're using. If you're using MySQL, it has native support for regexp

                expression regexp pattern
                  Write a Reply...