luzrain/ipcalc

IPv4/IPv6 Network calculator for PHP

v2.0.0 2025-07-04 10:12 UTC

This package is auto-updated.

Last update: 2025-07-04 13:14:55 UTC


README

PHP >=8.0 Tests Status Downloads

This IP Network Calculator library supports both IPv4 and IPv6, offering functionality to compute usable host ranges, retrieve subnet masks, and determine whether a given IP address belongs to a specific network.

Installation

Install with composer:

$ composer require luzrain/ipcalc

Usage

Create IPCalc\IP instance

$net = new IPCalc\IP('192.168.1.1/24');
// or
$net = new IPCalc\IP('192.168.1.1', 24);

You can then retrieve various properties of the network:

$net->getIp();                  // 192.168.1.1    // The original IP address
$net->getNetmask();             // 255.255.255.0  // Subnet mask
$net->getCidr();                // 24             // CIDR prefix length
$net->getVersion();             // 4              // IP version (4 or 6)
$net->isPrivate();              // true           // Returns true if the IP address is in a private range
$net->getNetwork();             // 192.168.1.0    // Network address of the subnet (IPv4 only)
$net->getBroadcast();           // 192.168.1.255  // Broadcast address of the network (IPv4 only)
$net->getHostMin();             // 192.168.1.1    // First usable IP address in the subnet
$net->getHostMax();             // 192.168.1.254  // Last usable IP address in the subnet
$net->contains('192.168.1.10'); // true           // Returns true if the given IP address is within the network

Notes:

Although IPv6 does not use the concept of networks and broadcasts, the ranges are still needed to do inclusive searches. Also, IPv6 has a subnet segment, but can still be supernetted/subnetted, which this takes into consideration.