Skip to content

dharple/ip-analysis

Repository files navigation

PHP IP Analyzer

Analyzes IP addresses to help programmatically identify IPs that are globally reachable, or not, and find out additional details about the IP.

Installation

If you're using composer, run:

composer require outsanity/ip-analysis

Usage

<?php

require 'vendor/autoload.php';

use Outsanity\IpAnalysis\IpAnalysis;

$ip = new IpAnalysis('127.0.0.1');
echo 'documentation: ' . ($ip->isDocumentation()  ? 'yes' : 'no') . "\n"; // 192.0.2.65, 2001:db8:1:3::2
echo 'global: ' .        ($ip->isGlobal()         ? 'yes' : 'no') . "\n"; // 8.8.8.8, 2001:4860:4860::8888
echo 'loopback: ' .      ($ip->isLoopback()       ? 'yes' : 'no') . "\n"; // 127.0.0.1, ::1
echo 'multicast: ' .     ($ip->isMulticast()      ? 'yes' : 'no') . "\n"; // 224.0.1.1, ff00::101
echo 'private: ' .       ($ip->isPrivateNetwork() ? 'yes' : 'no') . "\n"; // 10.0.0.1, 192.168.0.1, fd11:1111:1111::1
echo 'subnet: ' .        ($ip->isLocalNetwork()   ? 'yes' : 'no') . "\n"; // 169.254.0.1, fe80::6450:6a14:93ba:de09

Expected output:

documentation: no
global: no
loopback: yes
multicast: no
private: no
subnet: no

Build Support

A second package, IP Analysis Helper, converts the IANA .csv rule file into SpecialAddressBlock entities. These live in Outsanity\IpAnalysis\SpecialAddressBlock\Factory::$allRaw.

Thanks

The rules come from the IANA IPv4 Special Address Registry and the IANA IPv6 Special Address Registry.

Additional help came from the Wikipedia Reserved IP Addresses page and the RIPE IPv6 Reference Card.