Search by

aranxo / netinfo

Aranxo

Static helpers for IPv4/IPv6 CIDR calculations, bogon detection, allow-list loading and client IP evaluation.

Package info

github.com/Aranxo/netinfo

pkg:composer/aranxo/netinfo

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.1.0 2026-11-19 17:22 UTC

This package is auto-updated.

Last update: 2026-11-19 17:53:53 UTC


README

A collection of Internet related functions for PHP: IPv4/IPv6 CIDR range calculation, CIDR intersection, bogon detection, allow-list loading, client IP evaluation and URL reachability checks. All methods are static and the package consists of a single, dependency-free class.

Installation

composer require aranxo/netinfo

Requirements

  • PHP ≥ 8.1
  • PHP extension gmp (used for IPv6 calculations)
  • No external Composer dependencies
  • Optional: PHP extension curl for NetInfo::urlExists()

Class Aranxo\NetInfo\NetInfo

Collection of Internet related functions. All methods in this class are static.

Regex helpers

Method Description
createIPv4Regex(array $cidrs): string Creates a regex pattern for matching a list of IPv4 CIDR ranges
createIPv6Regex(array $cidrs): string Creates a regex pattern for matching a list of IPv6 CIDR ranges

IP ranges

Method Description
ipRangeIPv4(string $cidr): array Checks a given IPv4 CIDR notation and gives back the lower and upper border of the matching IP range as two element array
ipRangeIPv6(string $cidr): array Checks a given IPv6 CIDR notation and gives back the lower and upper border of the matching IP range as two element array
ipRange(string $cidr): array Checks a given CIDR notation and gives back the lower and upper border of the matching IP range as two element array
getAllIPv4sInCIDR(string $cidr): array Returns all IPv4 addresses contained in a given CIDR range as array
getAllIPv6sInCIDR(string $cidr): array Returns all IPv6 addresses contained in a given CIDR range as array
getAllIPsInCIDR(string $cidr): array Returns all IP addresses contained in a given CIDR range as array

CIDR calculation

Method Description
calcIPv4CIDR(array $ips): string Calculates the IPv4 CIDR notation which matches all IPv4s in a given array
calcIPv6CIDR(array $ips): string Calculates the IPv6 CIDR notation which matches all IPv6s in a given array
calcCIDR(array $ips): string Calculates the CIDR notation which matches all IPs in a given array

CIDR intersection

Method Description
intersectIPv4CIDRs(string $cidr1, string $cidr2): bool Calculates if an IPv4 is a member of a CIDR or one of two CIDRs is a subset of the other
intersectIPv6CIDRs(string $cidr1, string $cidr2): bool Calculates if an IPv6 is a member of a CIDR or one of two IPv6 CIDRs is a subset of the other
intersectCIDRs(string $cidr1, string $cidr2): bool Calculates if an IP is a member of a CIDR or one of two CIDRs is a subset of the other

Bogon detection

Method Description
isBogonIPv4(string $cidr): bool Checks if a given IPv4 or IPv4 CIDR is a bogon
isBogonIPv6(string $cidr): bool Checks if a given IPv6 or IPv6 CIDR is a bogon
isBogonIP(string $cidr): bool Checks if a given IP or CIDR is a bogon
getBogonIPv4CIDRs(): array Gets all bogon IPv4 CIDR ranges
getBogonIPv6CIDRs(): array Gets all bogon IPv6 CIDR ranges
getBogonCIDRs(): array Gets all bogon CIDR ranges (IPv4 and IPv6 combined)

Allow lists

Method Description
loadAllowedCIDRList(string $iplist): array Loads the allowed CIDRs from a JSON file (expected format: {"ips": {"CIDR": "description"}})
loadAllowedAgentsList(string $iplist): array Loads the allowed agents from a JSON file (expected format: {"agents": {"agent": "description"}})

Request / URL helpers

Method Description
getRealIP(): string Evaluates the real IP address of the current user
urlExists(string $url): bool Checks if a given url really exists and is reachable

Invalid IP or CIDR notations passed to the range, listing and intersection methods throw an \InvalidArgumentException.

Usage

<?php

require 'vendor/autoload.php';

use Aranxo\NetInfo\NetInfo;

[$first, $last] = NetInfo::ipRange('192.0.2.0/29');          // ['192.0.2.0', '192.0.2.7']
$cidr = NetInfo::calcCIDR(['64.15.129.100', '64.15.129.126']); // '64.15.129.96/27'
$inside = NetInfo::intersectCIDRs('64.15.129.100', $cidr);    // true
$bogon = NetInfo::isBogonIP('10.1.2.3');                       // true

echo "$first - $last, $cidr, ", var_export($inside, true), ', ', var_export($bogon, true), PHP_EOL;

Testing & static analysis

composer install
composer test      # PHPUnit
composer analyse   # PHPStan, level 8

License

MIT – see LICENSE.