note 74656 added to ref.network
| From: | claudiu at cnixs dot com | Date: | Sun, 22 Apr 2007 09:52:25 +0000 |
| Subject: | note 74656 added to ref.network | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125763@lists.php.net to get a copy of this message | ||
A simple and very fast function to check against CIDR.
Your previous examples are too complicated and involves a lot of functions call.
Here it is (only with arithmetic operators and call only to ip2long () and split() ):
<?php
function ipCIDRCheck ($IP, $CIDR) {
list ($net, $mask) = split ("/", $CIDR);
$ip_net = ip2long ($net);
$ip_mask = ~((1 << (32 - $mask)) - 1);
$ip_ip = ip2long ($IP);
$ip_ip_net = $ip_ip & $ip_mask;
return ($ip_ip_net == $ip_net);
}
?>
call example: <?php echo ipCheck ("192.168.1.23", "192.168.1.0/24"); ?>
----
Server IP: 69.147.83.197
Probable Submitter: 80.97.92.73
----
Manual Page -- http://www.php.net/manual/en/ref.network.php
Edit -- https://master.php.net/note/edit/74656
Del: integrated -- https://master.php.net/note/delete/74656/integrated
Del: useless -- https://master.php.net/note/delete/74656/useless
Del: bad code -- https://master.php.net/note/delete/74656/bad+code
Del: spam -- https://master.php.net/note/delete/74656/spam
Del: non-english -- https://master.php.net/note/delete/74656/non-english
Del: in docs -- https://master.php.net/note/delete/74656/in+docs
Del: other reasons-- https://master.php.net/note/delete/74656
Reject -- https://master.php.net/note/reject/74656
Search -- https://master.php.net/manage/user-notes.php