Skip to content

Small set of classes for identifying and enumerating IPv4 addresses and subnets in PHP

Notifications You must be signed in to change notification settings

tapmodo/php-ipv4

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 

Repository files navigation

IPv4 classes for PHP5

Purpose

Identify, convert, and enumerate IPv4 IP addresses and subnets

Examples

$ip = Ipv4_Address::fromString('10.2.1.1');
$sn = Ipv4_Subnet::fromString('10.2.0.0/16');

// Test if IP is in subnet
$sn->contains($ip)          // true
$sn->contains('10.3.1.23')  // false
Ipv4_Subnet::ContainsAddress($sn,$ip)
Ipv4_Subnet::ContainsAddress('192.168.1.0/27','192.168.1.246')

// Test if two IPs are on the same network
$netmask = '255.255.255.0';
Ipv4_Subnet::ContainsAddress(new Ipv4_Subnet($ip1,$netmask),$ip2)

// Can be written in numerous ways...
Ipv4_Subnet::ContainsAddress("{$ip1}/24",$ip2)
Ipv4_Subnet::fromString("{$ip1}/24")->contains($ip2)

// Subnet information
$sn->getNetwork()
$sn->getNetmask()
$sn->getNetmaskCidr()
$sn->getFirstHostAddr()
$sn->getLastHostAddr()
$sn->getBroadcastAddr()

// Enumerate subnet addresses
$ips = $sn->getIterator();
foreach($ips as $addr) ...

// Count number of usable IPs on subnet (implements Countable)
$sn->getTotalHosts()
$sn->count()
count($sn)

About

Small set of classes for identifying and enumerating IPv4 addresses and subnets in PHP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages