note 37786 added to function.ip2long
| From: | jbothe at hotmail dot com | Date: | Thu, 27 Nov 2003 04:01:11 +0000 |
| Subject: | note 37786 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-61014@lists.php.net to get a copy of this message | ||
The following script will print out subnet statistics when you supply the IP Address and Subnet
Mask. Usefull to calculate the broadcast address and network address as well as the number of hosts
and the range of IP addresses in the subnet.
#!/usr/local/bin/php
<?
$ip_addr = "172.14.1.57";
$subnet_mask = "255.255.255.0";
$ip = ip2long($ip_addr);
$nm = ip2long($subnet_mask);
$nw = ($ip & $nm);
$bc = $nw | (~$nm);
echo "IP Address: " . long2ip($ip) . "\n";
echo "Subnet Mask: " . long2ip($nm) . "\n";
echo "Network Address: " . long2ip($nw) . "\n";
echo "Broadcast Address: " . long2ip($bc) . "\n";
echo "Number of Hosts: " . ($bc - $nw - 1) . "\n";
echo "Host Range: " . long2ip($nw + 1) . " -> " . long2ip($bc - 1)
. "\n";
?>
Produces the output:
IP Address: 172.14.1.57
Subnet Mask: 255.255.255.0
Network Address: 172.14.1.0
Broadcast Address: 172.14.1.255
Number of Hosts: 254
Host Range: 172.14.1.1 -> 172.14.1.254
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+37786
Delete -- http://master.php.net/manage/user-notes.php?action=delete+37786&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+37786&report=yes
Search -- http://master.php.net/manage/user-notes.php