note 92544 added to function.ip2long
| From: | jwadhams1 at yahoo dot com | Date: | Tue, 28 Jul 2009 22:41:46 +0000 |
| Subject: | note 92544 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158846@lists.php.net to get a copy of this message | ||
I wanted to build on what kaputt and spinyn contributed in a way that I think is a little more
intuitive (e.g., let sprintf do all the binary conversion and padding, and let substr_compare do the
trimming and comparing):
function ip_in_network($ip, $net_addr, $net_mask){
if($net_mask <= 0){ return false; }
$ip_binary_string = sprintf("%032b",ip2long($ip));
$net_binary_string = sprintf("%032b",ip2long($net_addr));
return (substr_compare($ip_binary_string,$net_binary_string,0,$net_mask) === 0);
}
ip_in_network("192.168.2.1","192.168.2.0",24);
//true
ip_in_network("192.168.6.93","192.168.0.0",16);
//true
ip_in_network("1.6.6.6","128.168.2.0",1);
//false
----
Server IP: 64.71.164.2
Probable Submitter: 209.131.48.66
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- https://master.php.net/note/edit/92544
Del: integrated -- https://master.php.net/note/delete/92544/integrated
Del: useless -- https://master.php.net/note/delete/92544/useless
Del: bad code -- https://master.php.net/note/delete/92544/bad+code
Del: spam -- https://master.php.net/note/delete/92544/spam
Del: non-english -- https://master.php.net/note/delete/92544/non-english
Del: in docs -- https://master.php.net/note/delete/92544/in+docs
Del: other reasons-- https://master.php.net/note/delete/92544
Reject -- https://master.php.net/note/reject/92544
Search -- https://master.php.net/manage/user-notes.php