note 71955 modified in function.ip2long by danbrown
| From: | danbrown@php.net | Date: | Fri, 05 Dec 2008 13:46:16 +0000 |
| Subject: | note 71955 modified in function.ip2long by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147704@lists.php.net to get a copy of this message | ||
Here's a simple IP address match checking function.
It takes 3 arguments: ip address to check (after ip2long), ip address to check against (after
ip2long), and mask to check against (integer 0-32).
Just make sure you perform ip2long on the ip addresses before inputting them to the function.
<?php
function match_ip($check_ip, $match_ip, $match_mask = 32)
{
for ($i = 0 ; $i < $match_mask ; $i++)
{
$n = pow(2, 31 - $i) ;
if (($n & $check_ip) != ($n & $match_ip))
{
return FALSE ;
}
}
return TRUE ;
}
?>
I've been looking for a function like this for a while, i hope it helps someone.
--was--
Here's a simple IP address match checking function.
It takes 3 arguments: ip address to check (after ip2long), ip address to check against (after
ip2long), and mask to check against (integer 0-32).
Just make sure you perform ip2long on the ip addresses before inputting them to the function.
function match_ip($check_ip, $match_ip, $match_mask = 32)
{
for ($i = 0 ; $i < $match_mask ; $i++)
{
$n = pow(2, 31 - $i) ;
if (($n & $check_ip) != ($n & $match_ip))
{
return FALSE ;
}
}
return TRUE ;
}
I've been looking for a function like this for a while, i hope it helps someone.
http://php.net/manual/en/function.ip2long.php