Comparing IP addresses
Moderator: General Moderators
Comparing IP addresses
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?
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?
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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) == "e;10."e; ||substr($ip, 0, 8) == "e;192.168."e;) {
$localip = 1;
} elseif (substr($ip, 0, 4) == "e;172."e;) {
$field = explode("e;."e;, $ip);
if ($fieldї1] >= 16 && $fieldї1] <= 31) {
$localip = 1;
}
}- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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
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;
}
}Please keep in mind I am writing this in my browser and the code is untested:
Just an example...
[PHENOM | PLEASE USE
Code: Select all
<?
// 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>=$long_start && $long_ip<=$long_end)
return TRUE; // $ip is within the range of $start and $end
else
return FALSE; // $ip is not in the specified range
}
?>[PHENOM | PLEASE USE
Code: Select all
TAGS WHEN POSTING PHP[/color][/size]
Sorry, thanks...In code snipplets ok only '4' pages. My bad to assume there would be morefeyd wrote:<smartass>
to find a "Code Snippet" ... hmmm ... look in the Code Snippet's board.
</smartass>
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.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.
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.