dxw/cidr
Composer 安装命令:
composer require dxw/cidr
包简介
IP address matching with CIDR ranges
README 文档
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 ofIPv4AddressandIPv6Address)
IPRange::Make(string $range): \Dxw\Result\Result<RangeBase>(RangeBaseis the abstract superclass ofIPv4RangeandIPv6Range)
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
dxw/cidr 适用场景与选型建议
dxw/cidr 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 114.66k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2015 年 06 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 dxw/cidr 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dxw/cidr 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 114.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 38
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-08