tiat/libs-ip

MIT-licensed PHP 8.5+ IPv4/IPv6 address, CIDR and classification utilities.

Maintainers

Package info

gitlab.com/tiat/libs/ip

Issues

pkg:composer/tiat/libs-ip

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

v1.0.0 2026-06-21 12:14 UTC

This package is auto-updated.

Last update: 2026-06-21 09:43:55 UTC


README

MIT-licensed PHP 8.5+ IPv4/IPv6 address, CIDR and classification utilities.

This package provides:

  • strict IPv4 parsing (rejects 127.1, octal, leading zeros, hex, decimal-long)
  • full IPv6 parsing with :: compression, embedded IPv4 and zone-id handling
  • canonical RFC 5952 IPv6 formatting (lowercase, longest-zero-run compression)
  • IANA special-purpose classification (isPublic / isPrivate / isLoopback / ...)
  • IPv4-mapped IPv6 (::ffff:a.b.c.d) classified by the embedded IPv4 — SSRF-safe
  • CIDR blocks: network, broadcast, size, membership
  • inclusive, order-agnostic range membership
  • comparison and ordering (IPv4 sorts before IPv6)
  • no-throw tryParse APIs for request, cache and routing paths
  • dependency-free: no GMP, no BCMath, no BigInt polyfill

Install

composer require tiat/libs-ip:^1.0

Representation

Addresses are held in their big-endian binary form: 4 bytes for IPv4, 16 bytes for IPv6. All comparison, range and CIDR maths runs on those bytes, so results are exact and family-uniform.

64-bit contract

IPv6 is a 128-bit family. This library performs IPv6 maths on the 16-byte binary form and requires a 64-bit runtime (PHP_INT_SIZE >= 8). On a 32-bit runtime, every IPv6 entry point fails fast with Tiat\Libs\Ip\UnsupportedPlatformException rather than silently producing wrong results. IPv4 continues to work on a 32-bit runtime.

Usage

use Tiat\Libs\Ip\Ip;
use Tiat\Libs\Ip\Cidr;
use Tiat\Libs\Ip\IpUtil;

Ip::parse('2001:db8::1')->toString();          // "2001:db8::1"
IpUtil::isPublic('::ffff:127.0.0.1');           // false (loopback)
IpUtil::isValid('127.1');                        // false (strict)
Cidr::parse('10.0.0.0/8')->contains(Ip::parse('10.1.2.3')); // true
IpUtil::within('10.0.0.5', '10.0.0.1', '10.0.0.10');        // true

Versioning

The package starts at 1.0.0 and follows semantic versioning.

License

MIT