當前位置: 妍妍網 > 碼農

強大PHP工具庫用處理IPv4和IPv6網路地址

2024-04-20碼農

用於處理網路地址(IPv4和IPv6)的PHP庫。

安裝

composer require s1lentium/iptools

使用

IP營運

IPv4

$ip = new IP('192.168.1.1');
echo $ip->version;// IPv4

IPv6

$ip = new IP('fc00::');
echo $ip->version; // IPv6

從整數、二進制和十六進制解析IP

echo (string)IP::parse(2130706433); // 127.0.0.1
echo (string)IP::parse('0b11000000101010000000000100000001'// 192.168.1.1
echo (string)IP::parse('0x0a000001'); // 10.0.0.1

或者

echo (string)IP::parseLong(2130706433); // 127.0.0.1
echo (string)IP::parseBin('11000000101010000000000100000001'); // 192.168.1.1
echo (string)IP::parseHex('0a000001'); // 10.0.0.1

將IP轉換為其他格式

echo IP::parse('192.168.1.1')->bin // 11000000101010000000000100000001
echo IP::parse('10.0.0.1')->hex // 0a000001
echo IP::parse('127.0.0.1')->long // 2130706433

其他

  • maxPrefixLength 地址表示中的最大位數:IPv4為32,IPv6為128。

  • octetsCount IP地址中的八位字節數:IPv4為4,IPv6為16

  • reversePointer 地址的反向DNS PTR的名稱:

  • echonew IP::parse('192.0.2.5')->reversePointer // 5.2.0.192.in-addr.arpa
    echonew IP::parse('2001:db8::567:89ab')->reversePointer // b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa

    網路操作

    echo Network::parse('192.0.0.1 255.0.0.0')->CIDR; // 192.0.0.0/8
    echo (string)Network::parse('192.0.0.1/8')->netmask; // 255.0.0.0
    echo (string)Network::parse('192.0.0.1'); // 192.0.0.1/32

    網路中的IP地址

    $excluded = Network::parse('192.0.0.0/8')->exclude(new IP('192.168.1.1'));
    foreach($excluded as $network) {
    echo (string)$network . '<br>';
    }

    192.0.0.0/9
    192.128.0.0/11
    192.160.0.0/13
    192.168.0.0/24
    192.168.1.0/32
    192.168.1.2/31
    ...
    192.192.0.0/10

    網路中的子網路

    $excluded = Network::parse('192.0.0.0/8')->exclude(new Network('192.168.1.0/24'));
    foreach($excluded as $network) {
    echo (string)$network . '<br>';
    }

    192.0.0.0/9
    192.128.0.0/11
    192.160.0.0/13
    192.168.0.0/24
    192.168.2.0/23
    ...
    192.192.0.0/10

    將網路拆分為相等的網路

    $networks = Network::parse('192.168.0.0/22')->moveTo('24');
    foreach ($networks as $network) {
    echo (string)$network . '<br>';
    }

    192.168.0.0/24
    192.168.1.0/24
    192.168.2.0/24
    192.168.3.0/24

    叠代網路IP地址

    $network = Network::parse('192.168.1.0/24');
    foreach($network as $ip) {
    echo (string)$ip . '<br>';
    }

    192.168.1.0
    ...
    192.168.1.255

    獲取網路主機地址範圍:

    $hosts = Network::parse('192.168.1.0/24')->hosts // Range(192.168.1.1, 192.168.1.254);
    foreach($hosts as $ip) {
    echo (string)$ip . '<br>';
    }

    192.168.1.1
    ...
    192.168.1.254

    計算網路IP地址

    echo count(Network::parse('192.168.1.0/24')) // 254

    靶場作業

    以不同格式定義範圍

    $range = new Range(new IP('192.168.1.0'), new IP('192.168.1.255'));
    $range = Range::parse('192.168.1.0-192.168.1.255');
    $range = Range::parse('192.168.1.*');
    $range = Range::parse('192.168.1.0/24');

    檢查IP是否在範圍內

    echo Range::parse('192.168.1.1-192.168.1.254')->contains(new IP('192.168.1.5')); // true
    echo Range::parse('::1-::ffff')->contains(new IP('::1234')); // true

    叠代範圍IP地址

    $range = Range::parse('192.168.1.1-192.168.1.254');
    foreach($range as $ip) {
    echo (string)$ip . '<br>';
    }

    192.168.1.1
    ...
    192.168.1.254

    獲取適合指定IP地址範圍的網路

    $networks = Range::parse('192.168.1.1-192.168.1.254')->getNetworks();
    foreach($networks as $network) {
    echo (string)$network . '<br>';
    }

    192.168.1.1/32
    192.168.1.2/31
    192.168.1.4/30
    192.168.1.8/29
    192.168.1.16/28
    192.168.1.32/27
    192.168.1.64/26
    192.168.1.128/26
    192.168.1.192/27
    192.168.1.224/28
    192.168.1.240/29
    192.168.1.248/30
    192.168.1.252/31
    192.168.1.254/32

    計算範圍內的IP地址

    echo count(Range::parse('192.168.1.1-192.168.1.254')) // 254