How to handle the IP address and subnet mask in PHP

Source: Internet
Author: User
Tags explode pow

The Ip2long IP address is converted to an integer type.
Long2ip integer data is converted to IP.

The subnet mask is converted to a mask length mode:
$slash _notation = strlen (Preg_replace ("/0/", "", Decbin (Ip2long ($subnet _mask)));
$bits =strpos (Decbin (Ip2long ($mask)), "0");

The subnet mask bit length is converted into a subnet mask form:
$mask = 0xFFFFFFFF << (32-$mask);
$mask = POW (2,32)-pow (2, (32-$mask));

Determine if two addresses are within a subnet:
<?php
function Matchcidr ($addr, $CIDR) {
List ($ip, $mask) = explode ('/', $CIDR);
Return (Ip2long ($addr) >> (32-$mask) = = Ip2long ($ip) >> (32-mask));
}
?>

A class of IPv4:
<?php

//--------------
IPv4 class
Class IPv4
{
var $address;
var $netbits;

//--------------
Create New Class
function IPv4 ($address, $netbits)
{
$this->address = $address;
$this->netbits = $netbits;
}

//--------------
Return the IP address
function address () {return ($this->address);}

//--------------
Return the Netbits
function Netbits () {return ($this->netbits);}

//--------------
Return the netmask
function netmask ()
{
Return (Long2ip (Ip2long ("255.255.255.255")
<< (32-$this->netbits)));
}

//--------------
Return the network, the address sits in
function Network ()
{
Return (Long2ip ((Ip2long ($this->address))
& (Ip2long ($this->netmask ())));
}

//--------------
Return the broadcast, the address sits in
function Broadcast ()
{
Return (Long2ip (Ip2long ($this->network ())
| (~ (Ip2long ($this->netmask ()))));
}

//--------------
Return the inverse mask of the netmask
function Inverse ()
{
Return (Long2ip (~ (Ip2long ("255.255.255.255")
<< (32-$this->netbits)));
}

}

$ip = new IPv4 ("192.168.2.1", 24);
Print "Address: $ip->address () \ n";
Print "Netbits: $ip->netbits () \ n";
Print "Netmask: $ip->netmask () \ n";
Print "Inverse: $ip->inverse () \ n";
Print "Network: $ip->network () \ n";
Print "Broadcast: $ip->broadcast () \ n";
?>

This approach is more creative:
For those poor little people using PHP 3, here's an ip2long:
<?php
if (!function_exists ("Ip2long")) {
function Ip2long ($IP) {
$ex = Explode (".", $ip);
if (count ($ex)!=4) return-1;
List ($a, $b, $c, $d) = $ex;
$a = $a *16777216;
$b = $b *65536;
$c = $c *256;
Return $a + $b + $c + $d;
}
}
?>

#!/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


How to handle the IP address and subnet mask in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.