note 70734 added to function.ip2long
| From: | jgwright at osu1 dot php dot net | Date: | Wed, 25 Oct 2006 19:13:36 +0000 |
| Subject: | note 70734 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119114@lists.php.net to get a copy of this message | ||
Here is a modified version of the code posted by legetz81 (AT) yahoo (dot) com. It handles the
shorter, and more common, notation: "189.128/11".
<?php
$ip_addr_cidr = "192.168.37.215/27";
cidr($ip_addr_cidr);
function cidr($ip_addr_cidr) {
$ip_arr = explode('/', $ip_addr_cidr);
$dotcount = substr_count($ip_arr[0], ".");
$padding = str_repeat(".0", 3 - $dotcount);
$ip_arr[0].=$padding;
$bin = '';
for($i=1;$i<=32;$i++) {
$bin .= $ip_arr[1] >= $i ? '1' : '0';
}
$ip_arr[1] = bindec($bin);
$ip = ip2long($ip_arr[0]);
$nm = ip2long($ip_arr[1]);
$nw = ($ip & $nm);
$bc = $nw | (~$nm);
echo "Number of Hosts: " . ($bc - $nw - 1) . "\n";
echo "Host Range: " . long2ip($nw + 1) . " -> " . long2ip($bc - 1) .
"\n";
/*
This will produce:
Number of Hosts: 30
Host Range: 192.168.37.193 -> 192.168.37.222
*/
}
?>
----
Server IP: 217.160.72.57
Probable Submitter: 58.186.139.55
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- https://master.php.net/note/edit/70734
Del: integrated -- https://master.php.net/note/delete/70734/integrated
Del: useless -- https://master.php.net/note/delete/70734/useless
Del: bad code -- https://master.php.net/note/delete/70734/bad+code
Del: spam -- https://master.php.net/note/delete/70734/spam
Del: non-english -- https://master.php.net/note/delete/70734/non-english
Del: in docs -- https://master.php.net/note/delete/70734/in+docs
Del: other reasons-- https://master.php.net/note/delete/70734
Reject -- https://master.php.net/note/reject/70734
Search -- https://master.php.net/manage/user-notes.php