Prev | Current Page 489 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

To
remedy this, a few PHP functions and bitwise operations can be coaxed into doing
the calculations for you. When provided an IP address and a bitmask, Listing 16-3
calculates several of these components.
CHAPTER 16 ?–  NETWORKING 421
Listing 16-3. A Subnet Converter


IP Address:

.
.
.



Subnet Mask:

.
.
.




if (isset($_POST['submit'])) {
// Concatenate the IP form components and convert to IPv4 format
$ip = implode('.', $_POST['ip']);
$ip = ip2long($ip);
// Concatenate the netmask form components and convert to IPv4 format
$netmask = implode('.', $_POST['sm']);
$netmask = ip2long($netmask);
// Calculate the network address
$na = ($ip & $netmask);
// Calculate the broadcast address
$ba = $na | (~$netmask);
422 CHAPTER 16 ?–  NE TWORKING
// Convert the addresses back to the dot-format representation and display
echo "Addressing Information:
";
echo "