Home » Networking » Net_IPv4 » Manual
Provides several methods for working with IP addresses and netmasks.
Net_IPv4::atoh()
Net_IPv4::atoh() – Converts a dot-quad formatted IP address into a hexadecimal string
Synopsis
require_once 'Net/IPv4.php';
mixed atoh (
string $addr
)
Description
Converts a dot-quad formatted IP address into a hexadecimal string - for example "192.168.1.0" to "c0a80100". Returns FALSE if the string given is no valid IP adress.
Note
This function can be called statically.
Net_IPv4::htoa()
Net_IPv4::htoa() – Converts a hexadecimal string into a dot-quad formatted IP address
Synopsis
require_once 'Net/IPv4.php';
mixed htoa (
string $addr
)
Description
Converts hexadecimal string to a dot-quad formatted IP address - for example "c0a80100" to "192.168.1.0". Returns FALSE if the hexadecimal string is no valid IP adress.
Note
This function can be called statically.
Net_IPv4::ip2double()
Net_IPv4::ip2double() – Converts an IP address to a PHP double.
Synopsis
require_once 'Net/IPv4.php';
float ip2double (
string $ip_addr
)
Description
Converts an IP address to a PHP double - for example "192.168.1.0" to "3232235776". A double is, in contrast to ip2long's integer, not signed and may be more comfortable when presented to the user. It can still be reconverted with PHP's long2ip().
Note
This function can be called statically.
Net_IPv4::validateIP()
Net_IPv4::validateIP() – Validate the syntax of the given IP adress.
Synopsis
require_once 'Net/IPv4.php';
bool validateIP (
string $ip_addr
)
Description
Validates the syntax of an IP address in dot-quad format. Returns true if address is valid, false if not.
Note
This function can be called statically.
Net_IPv4::validateNetmask()
Net_IPv4::validateNetmask() – Validate the syntax of the given netmask.
Synopsis
require_once 'Net/IPv4.php';
bool validateNetmask (
string $netmask
)
Description
Validates the syntax of a dot-quad formatted netmask. Returns true if netmask is valid, false if not.
Note
This function can be called statically.
Net_IPv4::parseAddress()
Net_IPv4::parseAddress() – Parse a CIDR address (address/netmask combination).
Synopsis
require_once('Net/IPv4.php');
bool parseAddress (
string $address
)
Description
Parses a Classless Inter-Domain Routing address (e.g. 192.168.1.0/24) and stores information in the object variables network, ip, netmask, broadcast, long and bitmask.
Example
How to get netmask and broadcast from CIDR address
<?php
require('Net/IPv4.php');
$cidr = '192.168.0.50/16';
$net = Net_IPv4::parseAddress($cidr);
echo $net->network; // 192.168.0.0
echo $net->ip; // 192.168.0.50
echo $net->broadcast; // 192.168.255.255
echo $net->bitmask; // 16
echo $net->long; // 3232235520 (long/double version of 192.168.0.50)
echo $net->netmask; // 255.255.0.0
?>
Return value
boolean - Returns TRUE on success,
PEAR_Error on failure.
Note
This function can not be called statically.
Net_IPv4::calculate()
Net_IPv4::calculate() – Calculates network information based on an IP address and netmask.
Synopsis
require_once 'Net/IPv4.php';
mixed calculate (
)
Description
Fully populates the object properties based on the IP address and netmask/bitmask properties. Once these two fields are populated, calculate() will perform calculations to determine the network and broadcast address of the network.
Example
Calculating broadcast and network address
<?php
require 'Net/IPv4.php';
// create IPv4 object
$ip_calc = new Net_IPv4();
// set variables
$ip_calc->ip = "192.168.1.10";
$ip_calc->netmask = "255.255.255.0";
/* alternative method with numerical values:
$ip_calc->long = 3232235786;
$ip_calc->bitmask = 24;
*/
// calculate
$error = $ip_calc->calculate();
if (!is_object($error))
{
// if returned true, output
echo "Your network address: ".$ip_calc->network."<br />";
echo "Your broadcast address: ".$ip_calc->broadcast."<br />";
}
else
{
// otherwise handle error
echo "An error occured: ".$error->getMessage();
}
?>
Return value
boolean - Returns TRUE on success,
PEAR_Error on failure.
Note
This function can not be called statically.
Net_IPv4::ipInNetwork()
Net_IPv4::ipInNetwork() – Determines whether or not the supplied IP is within the supplied network.
Synopsis
require_once 'Net/IPv4.php';
bool ipInNetwork (
string $ip
, string $cidr_block
)
Description
Determines whether or not the supplied IP address is within the supplied network.
Example
Checking if a IP adress is contained in a network
<?php
require 'Net/IPv4.php';
$ip = '10.11.12.13';
$net1 = '10.0.0.1/8';
$net2 = '127.0.0.1/8';
echo Net_IPv4::ipInNetwork($ip, $net1) // bool(true)
echo Net_IPv4::ipInNetwork($ip, $net2) // bool(false)
?>
Note
This function can be called statically.