dxw / cidr
IP address matching with CIDR ranges
Installs: 99 790
Dependents: 2
Suggesters: 0
Security: 0
Stars: 7
Watchers: 20
Forks: 4
Open Issues: 4
pkg:composer/dxw/cidr
Requires
- php: ^7.4||^8.1
- dxw/result: ^1.0
- phpseclib/phpseclib: ^2.0
Requires (Dev)
- dxw/php-cs-fixer-config: ^2.0
- kahlan/kahlan: ^5.2
- vimeo/psalm: ^5.14
- dev-main
- v3.2.0
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.1
- v3.0.0
- v2.0.0
- v1.0.0
- dev-dependabot/github_actions/actions/cache-4
- dev-feature/php8
- dev-chore/migrate-to-github-actions
- dev-renovate/configure
- dev-chore/remove-composer-lock
- dev-chore/composer-v2-compatibility
- dev-repo-audit/add-contributing-documentation
- dev-feature/add-php-version-requirement
- dev-feature/psalm
- dev-use-ipv6
- dev-feature/use-error-values
- dev-feature/ipv6
This package is auto-updated.
Last update: 2025-10-13 13:04:12 UTC
README
PHP library for matching an IP address to a CIDR range.
Supports IPv4 and IPv6.
Installation
composer require dxw/cidr
Usage
To simply match two addresses:
$result = \Dxw\CIDR\IP::contains('2001:db8:123::/64', '2001:db8:123::42');
if ($result->isErr()) {
    // handle the error
}
$match = $result->unwrap();
if ($match) {
    echo "The addresses match!\n";
} else {
    echo "The addresses don't match.\n";
}
Notes
IPv4-compatible IPv6 addresses and IPv4-mapped IPv6 addresses are partially supported.
An address of the form ::127.0.0.1 or ::ffff:127.0.0.1 will be parsed. But only if they fall within ::/96 or ::ffff:0:0/96. For example, 2001:db8::127.0.0.1 will be rejected.
But the resulting address will be treated as an IPv6 and as such it will never match an IPv4 address. For example, 127.0.0.1 will never match ::ffff:127.0.0.1 or ::127.0.0.1.
API
Example of testing if an IPv6 address falls within a particular IPv6 range:
$result = \Dxw\CIDR\IPv6Range::Make('2001:db8:123::/64');
if ($result->isErr()) {
    // handle the error
}
$range = $result->unwrap();
$result = \Dxw\CIDR\IPv6Address::Make('2001:db8:123::42');
if ($result->isErr()) {
    // handle the error
}
$address = $result->unwrap();
if ($range->containsAddress($address)) {
    echo "It matches!\n";
} else {
    echo "It doesn't match.\n";
}
To make the example IPv4-only, replace IPv6 with IPv4. To make the example version agnostic, replace IPv6 with just IP.
- IP- ::contains(string $addressOrRange, string $address): \Dxw\Result\Result<bool>
 
- IPAddress- ::Make(string $address): \Dxw\Result\Result<AddressBase>(- AddressBaseis the abstract superclass of- IPv4Addressand- IPv6Address)
 
- IPRange- ::Make(string $range): \Dxw\Result\Result<RangeBase>(- RangeBaseis the abstract superclass of- IPv4Rangeand- IPv6Range)
 
- IPv4Address- ::Make(string $address): \Dxw\Result\Result<IPv4Address>
- ::FromBinary(\phpseclib\Math\BigInteger $binary): \Dxw\Result\Result<IPv4Address>
- ->__toString(): string
- ->getBinary(): \phpseclib\Math\BigInteger
 
- IPv6Address- ::Make(string $address): \Dxw\Result\Result<IPv6Address>
- ::FromBinary(\phpseclib\Math\BigInteger $binary): \Dxw\Result\Result<IPv6Address>
- ->__toString(): string
- ->getBinary(): \phpseclib\Math\BigInteger
 
- IPv4Block- ::Make(int $value): \Dxw\Result\Result<IPv4Block>
- ->__toString(): string
- ->getValue(): int
- ->getNetmask(): \phpseclib\Math\BigInteger
 
- IPv6Block- ::Make(int $value): \Dxw\Result\Result<IPv6Block>
- ->__toString(): string
- ->getValue(): int
- ->getNetmask(): \phpseclib\Math\BigInteger
 
- IPv4Range- ::Make(string $range): \Dxw\Result\Result<IPv4Range>
- ->__toString(): string
- ->getAddress(): \Dxw\CIDR\IPv4Address
- ->getBlock(): \Dxw\CIDR\IPv4Block
- ->containsAddress(\Dxw\CIDR\AddressBase $address): bool
 
- IPv6Range- ::Make(string $range): \Dxw\Result\Result<IPv6Range>
- ->__toString(): string
- ->getAddress(): \Dxw\CIDR\IPv6Address
- ->getBlock(): \Dxw\CIDR\IPv6Block
- ->containsAddress(\Dxw\CIDR\AddressBase $address): bool