PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Networking » Net_CheckIP2 » Manual

A package to check the correct syntax of an IPv4 address, to determine whether the IP is from a private/reserved or the zeroconf pool, and the IP's class network (A, B and C are currently supported).

Net_CheckIP2::isValid()

Net_CheckIP2::isValid() – Validation of IPv4 addresses

Synopsis

require_once 'Net/CheckIP2.php';

boolean Net_CheckIP2::isValid ( string $ip )

Description

This function can validate if a given string has a valid IPv4 syntax.

Parameter

Return value

boolean - TRUE, if syntax is valid

Note

This function can be called statically.

Example

Using isValid()

<?php
require_once 'Net_CheckIP2/CheckIP.php';

if (Net_CheckIP::isValid("your_ip_goes_here")) {
    // Syntax of the IP is ok
}
?>

From Net_CheckIP to Net_CheckIP2

Net_CheckIP2 is meant to replace the Net_CheckIP package with a couple small changes:

Old

<?php
require_once 'Net/CheckIP.php';
$ip = '127.0.0.1';
if (Net_CheckIP::check_ip($ip) {
    echo 'This is a valid IPv4 address!';
}
?>

New

<?php
require_once 'Net/CheckIP2.php';
$ip = '127.0.0.1';
if (Net_CheckIP2::isValid($ip) {
    echo 'This is a valid IPv4 address!';
}
?>

Class networks

Net_CheckIP2 has a getClass method which helps the developer to determine if the IP is within an class A, B or C network.

In case getClass is unable to determine the class, or the IP is invalid, false is returned.

getClass

<?php
require_once 'Net/CheckIP.php';
$ip = '127.0.0.1';

$classNetwork = Net_CheckIP::getClass($ip);
if ($classNetwork === false) {
    die('Unable to determine the class. The IP might be invalid also.');
}
switch ($classNetwork) {
case Net_CheckIP2::CLASS_A:
    echo 'Class A network.';
    break;
case Net_CheckIP2::CLASS_B:
    echo 'Class B network.';
    break;
case Net_CheckIP2::CLASS_C:
    echo 'Class C network.';
    break;
}
?>

Net_CheckIP2::is*()

Along with isValid()(), Net_CheckIP2 offers a couple methods to determine the IP's whereabouts. Currently implemented are methods to check if an IP is from a reserved IP space or the zeroconf pool.

isReserved()

Checks if the IP address is reserved (according to RFC1918).

isReserved()

<?php
require_once 'Net/CheckIP.php';
$ip = '127.0.0.1';
var_dump(Net_CheckIP::isReserved($ip));
?>

isZeroconf()

Zeroconf - automatically created usable IPs without manual intervention or management/configuration servers. This includes IPs from 169.254.0.0 to 169.254.255.255. (RFC3330)

isZeroConf()

<?php
require_once 'Net/CheckIP.php';
$ip = '127.0.0.1';
var_dump(Net_CheckIP::isZeroconf($ip));
?>