IPLib

A zero-dependency, PSR-compliant PHP library for parsing and manipulating IPv4/IPv6 addresses and ranges

Library
Composer
v1.22.0
313stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
41/100Fair
Development Activity0
Maintenance20
Community64
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
75/100Good
Architecture75
Code Quality78
Innovation68
Learning Curve80

IPLib is a modern, test-driven PHP library that implements primitives to handle IPv4 and IPv6 addresses along with IP ranges expressed as CIDR subnets, asterisk patterns, or single addresses. It works with any PHP version from 5.3.3 through 8.x, has no external dependencies, and requires no special PHP configuration — IPv6 support works even on builds without native IPv6.

The library provides address parsing and normalization, arithmetic operations like shifting and adding addresses, range containment checks, boundary calculations to derive the smallest range spanning two addresses, and address-type classification (loopback, private network, public). It is widely used by projects that need reliable, dependency-free IP handling such as access-control lists, geolocation preprocessing, and network validation logic.

What You Get

  • Parsing for IPv4 and IPv6 addresses via Factory::parseAddressString() with format auto-detection
  • Range parsing for CIDR subnets, asterisk patterns, and single addresses
  • Address arithmetic: get next/previous address, shift bits, add two addresses, compute address at offset
  • Range containment checks (contains, matches, containsRange) between addresses and ranges
  • Boundary and coverage utilities to derive the minimal range spanning a set of addresses
  • Address-type classification such as loopback, private network, unspecified, and public

Common Use Cases

  • Validating and normalizing user-submitted IP addresses in web forms or APIs
  • Implementing IP allow/deny lists and access-control rules with CIDR or pattern matching
  • Calculating the minimal CIDR block(s) that cover a set of known addresses for firewall or routing configuration
  • Classifying request IPs as public, private, or loopback for logging and security middleware

Under The Hood

Architecture — The library is organized around three interface families under src/: Address (IPv4/IPv6 parsing and arithmetic), Range (Subnet/Pattern/Single range types implementing a shared RangeInterface), and a Factory facade (src/Factory.php, 342 lines) that auto-detects format and dispatches to the correct concrete class. Low-level bit math is isolated in src/Service/ (BinaryMath, UnsignedIntegerMath, NumberInChunks), keeping arbitrary-precision arithmetic separate from the address/range domain model. Tech Stack — Pure PHP with no runtime dependencies beyond php >=5.3.3 (composer.json); PHPUnit 4.8 through 9.5 is supported for testing across that wide PHP range, plus ext-pdo_sqlite for a boundary-data test fixture, and PHPStan is configured at level: max for static analysis. Code Quality — 43 test files under test/tests/ cover addresses, ranges, and membership scenarios; src/Address/IPv4.php (582 lines) and IPv6.php (679 lines) are large but cohesive single-responsibility classes, and PHPStan max-level analysis plus a php-cs-fixer config enforce consistent style across the two-decade PHP version span it supports. API Design — The Factory::parseAddressString()/parseRangeString() entry points auto-detect IPv4 vs IPv6 and range notation, minimizing boilerplate for callers, while explicit IPv4/IPv6/Subnet/Pattern classes are available when the format is already known; __toString() implementations on address and range objects make output ergonomic.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search