note 56373 added to function.ip2long
| From: | ken at expitrans dot com | Date: | Wed, 31 Aug 2005 22:31:52 +0000 |
| Subject: | note 56373 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94551@lists.php.net to get a copy of this message | ||
Below is a merged form of all various notes, and a better (and correct) network matching function.
<?php
function net_match($network, $ip) {
// determines if a network in the form of 192.168.17.1/16 or
// 127.0.0.1/255.255.255.255 or 10.0.0.1 matches a given ip
$ip_arr = explode('/', $network);
$network_long = ip2long($ip_arr[0]);
$x = ip2long($ip_arr[1]);
$mask = long2ip($x) == $ip_arr[1] ? $x : 0xffffffff << (32 - $ip_arr[1]);
$ip_long = ip2long($ip);
// echo ">".$ip_arr[1]."> ".decbin($mask)."\n";
return ($ip_long & $mask) == ($network_long & $mask);
}
echo net_match('192.168.17.1/16', '192.168.15.1')."\n"; // returns
true
echo net_match('127.0.0.1/255.255.255.255', '127.0.0.2')."\n"; //
returns false
echo net_match('10.0.0.1', '10.0.0.1')."\n"; // returns true
?>
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56373
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56373&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56373&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56373&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56373&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56373&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56373&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56373&report=yes
Search -- http://master.php.net/manage/user-notes.php