note 37786 modified in function.ip2long by danbrown
| From: | danbrown@php.net | Date: | Fri, 05 Dec 2008 13:46:36 +0000 |
| Subject: | note 37786 modified in function.ip2long by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147706@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
<?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
--was--
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
http://php.net/manual/en/function.ip2long.php