note 41576 added to function.ip2long
| From: | dzver at rn2 dot php dot net | Date: | Thu, 15 Apr 2004 09:12:10 +0000 |
| Subject: | note 41576 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-68155@lists.php.net to get a copy of this message | ||
Mix of rbsmith's function and pasted above url:
<?
// returns 1 if $ip is part of $network
function IP_Match($network, $ip) {
$ip_arr = explode("/",$network);
$network_long=ip2long($ip_arr[0]);
$mask_long= pow(2,32)-pow(2,(32-$ip_arr[1]));
$ip_long=ip2long($ip);
if (($ip_long & $mask_long) == $network_long) {
return 1;
} else {
return 0;
}
}
// usage
$network="200.100.50.0/23";
$ip="200.100.51.55";
$ip2="200.100.52.2";
echo IP_Match($network, $ip); //prints 1
echo IP_Match($network, $ip2); //prints 0
?>
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41576
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41576&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41576&report=yes
Search -- http://master.php.net/manage/user-notes.php