Comparing IP addresses

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Comparing IP addresses

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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:
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post 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;
       }
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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;
    }
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

pssst... there's a code snippet for this.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post 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?
biz0r
Forum Newbie
Posts: 13
Joined: Mon Oct 27, 2003 3:21 pm
Location: Houston, TX

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post 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.
Post Reply