gwk/ip_address

IP地址处理库

0.2 2016-02-26 12:05 UTC

This package is not auto-updated.

Last update: 2024-09-28 13:30:21 UTC


README

此库允许您以各种形式处理IP地址(目前仅支持IPv4)并测试IP地址是否符合某些约束(相等性,是否在子网或范围内)。

示例

<?php

use IPAddress\IPv4\Address;
use IPAddress\IPv4\Subnet;

$address = new Address("1.2.8.200"); // These statements yield the same address
$address = new Address(0x010208c8);
$address = new Address(array(1, 2, 8, 200));
$address = new Address(array("1", "2", "010", "0xc8"));

$subnet = new Subnet("1.2.3.4", "255.255.0.0"); // These statements yield the same subnet
$subnet = Subnet::fromCidr("1.2.3.4", 16);
$subnet = Subnet::fromString("1.2.3.4 255.255.0.0");
$subnet = Subnet::fromString("1.2.3.4/16");

if($address->isPrivate()) {
    echo "Your address is in one of the RFC1918 Private networks.\n";
}

if($subnet->match($address)) {
    echo "I know your address is in the \"$subnet\" subnet.\n";
    // OUTPUT: I know your address is in the "1.2.3.4 netmask 255.255.0.0" subnet.
}

该库的测试已在32位和64位php上运行,应能在两者上工作。请注意,Address类的int()方法(返回IP地址的整数表示)在32位php上对于大于127.255.255.255的IP地址将返回负数,因为php不支持无符号整数。

待办事项

  • 更多实用方法
  • IPv6支持

免责声明

我对IP地址的了解有限,因此我可能在术语或库在某些情况下的行为方面犯了一些错误。

许可证

MIT,请参阅LICENSE