Page 1 of 1

Comparing IP addresses

Posted: Sun Mar 20, 2005 4:10 am
by AGISB
What is the best was to check if an IP is in a certain range.

I want to check if an IP is in the local ranges of 10.x.x.x, 172.16.x.x - 172.31.x.x or 192.168.x.x

Is there a better way than exploding by '.' and comparing the blocks?

Posted: Sun Mar 20, 2005 4:19 am
by Chris Corbyn
Using explode (or a regular expression) seems good to me. Either will work, except a regular expression may help you to validate the IP address more efficiently :wink:

Posted: Sun Mar 20, 2005 4:33 am
by AGISB
This is what I got for now. I still think there might be something faster.

Code: Select all

$localip = 0;
if (substr($ip, 0, 3) == &quote;10.&quote; ||substr($ip, 0, 8) == &quote;192.168.&quote;) {
      $localip = 1;
} elseif (substr($ip, 0, 4) == &quote;172.&quote;) {
       $field = explode(&quote;.&quote;, $ip);
       if ($field&#1111;1] >= 16 && $field&#1111;1] <= 31) {
               $localip = 1;
       }
}

Posted: Sun Mar 20, 2005 5:51 am
by Chris Corbyn
Maybe a regular expression could help, but as I say it has much the same effect. the only difference is that you can pull ALL of what you need out of just one pattern match :wink:

Code: Select all

$localip = 0;
preg_match('/^((\d{1,3}\.)(\d{1,3})\.)\d{1,3}\.\d{1,3}$/', $ip, $ipmatch);
if ($ipmatch[2] == '10.' || $ipmatch[1] == '192.168.') {
    $localip = 1;
} elseif ($ipmatch[2] == '172.') {
    if ($ipmatch[3] >= 16 && $ipmatch[3] <= 31) {
        $localip = 1;
    }
}

Posted: Sun Mar 20, 2005 8:58 am
by feyd
pssst... there's a code snippet for this.

Posted: Sun Mar 20, 2005 10:10 am
by timvw
i would convert them the 32bit presentation... and then see if the first 8,16,24 (or whatever the netblock is) bits are the same.

Posted: Sun Mar 20, 2005 12:11 pm
by AGISB
feyd wrote:pssst... there's a code snippet for this.
I did a search for ip but got no results. How should I find it?

Posted: Sun Mar 20, 2005 12:44 pm
by biz0r
Please keep in mind I am writing this in my browser and the code is untested:

Code: Select all

&lt;?
// Here is a very long way of writing out what you want (wrote it this way for descriptive
// purposes)
function ip_in_range($ip,$start, $end)
{
  // First convert the IP addresses into their respective LONG INT values
  $long_start=ip2long($start);
  $long_end=ip2long($end);
  $long_ip=ip2long($ip);

  if($long_ip&gt;=$long_start &amp;&amp; $long_ip&lt;=$long_end)
    return TRUE; // $ip is within the range of $start and $end
  else
    return FALSE; // $ip is not in the specified range
}
?&gt;
Just an example...

[PHENOM | PLEASE USE

Code: Select all

TAGS WHEN POSTING PHP[/color][/size]

Sorry, thanks...

Posted: Sun Mar 20, 2005 5:01 pm
by feyd
<smartass>
to find a "Code Snippet" ... hmmm ... look in the Code Snippet's board.
</smartass>


searching the site for 'ip' will return nothing because, well mysql doesn't search for anything less than or equal to 2 or 3 characters in length.

Posted: Mon Mar 21, 2005 12:44 am
by AGISB
feyd wrote:<smartass>
to find a "Code Snippet" ... hmmm ... look in the Code Snippet's board.
</smartass>
In code snipplets ok only '4' pages. My bad to assume there would be more
feyd wrote: searching the site for 'ip' will return nothing because, well mysql doesn't
search for anything less than or equal to 2 or 3 characters in length.
I know this. However if you do a search on 'ip' you get results and many of them. This did throw me off and actually made me think you guys use a different DB. So I assumed there were no fitting topics when I scanned over the results.

By the way that code snipplet I found in 'Code Snipplets' does not fit this task on hand at all. This is snipplet is not fit for the web. I am trying to determine the first web address in an array including Remote_Addr and HTTP_X_FORWARDED_FOR

As this will be part of my session management I want it to execute as fast as possible.