detain/iptools
Composer 安装命令:
composer require detain/iptools
包简介
PHP Library for manipulating network addresses (IPv4 and IPv6)
README 文档
README
PHP Library for manipulating network addresses (IPv4 and IPv6).
Changes since fork
- 100% Code Coverage!
- Now Testing On PHP v5.6, v7.0, v7.1, v7.2, v7.3, and v7.4
- Added CodeClimate Coverage Reporting
- Added containsAny and containsAll to Range class for dealing with an array of IP/Range/Networks instead of a single one
- Added excludeArray to the Network class for dealing with an array of IP/Range/Networks to exclude instead of the single one with the existing exclude method
Installation
Composer: Run in command line:
composer require detain/iptools
or put in composer.json:
{
"require": {
"detain/iptools": "*"
}
}
Usage
This library breaks its handling down into IP, Range, and Network types and provdes a matching named class to make working with all things IP much simpler.
IP Operations
$ip = new IP('192.168.1.1'); echo $ip->version;// IPv4
$ip = new IP('fc00::'); echo $ip->version; // IPv6
Parsing IP from integer, binary and hex:
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
or:
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
Converting IP to other formats:
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
Other public properties:
maxPrefixLength
The max number of bits in the address representation: 32 for IPv4, 128 for IPv6.
octetsCount
The count of octets in IP address: 4 for IPv4, 16 for IPv6
reversePointer
The name of the reverse DNS PTR for the address:
echo new IP::parse('192.0.2.5')->reversePointer // 5.2.0.192.in-addr.arpa echo new 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
Network Operations
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
Exclude IP from Network:
$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
Exclude Subnet from Network:
$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
Exclude Many Subnets from Network:
$excluded = Network::parse('192.0.0.0/8')->excludeArray(['192.168.1.0/24', '192.168.2.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.3.0/23
...
192.192.0.0/10
Split network into equal subnets
$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
Iterate over Network IP adresses:
$network = Network::parse('192.168.1.0/24'); foreach($network as $ip) { echo (string)$ip . '<br>'; }
192.168.1.0
...
192.168.1.255
Get Network hosts adresses as Range:
$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
Count Network IP adresses
echo count(Network::parse('192.168.1.0/24')) // 254
Range Operations
Define the range in different formats:
$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');
Check if IP is within Range:
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
Check if IP is within any of the Ranges in an array:
echo Range::parse('192.168.1.1-192.168.1.254')->containsAny([new IP('192.168.0.5'), new IP('192.168.1.5')]); // true
Check if IP is within all of the Ranges in an array:
echo Range::parse('192.168.1.1-192.168.1.254')->containsAll([new IP('192.168.0.5'), new IP('192.168.1.5')]); // false echo Range::parse('192.168.1.1-192.168.1.254')->containsAll([new IP('192.168.1.6'), new IP('192.168.1.5')]); // true
Iterate over Range IP adresses:
$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
Get Networks that fit into a specified range of IP Adresses:
$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
Count IP adresses in Range
echo count(Range::parse('192.168.1.1-192.168.1.254')) // 254
License
The library is released under the MIT.
detain/iptools 适用场景与选型建议
detain/iptools 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.88k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 04 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「network」 「ipv6」 「IP」 「ipv4」 「subnet」 「cidr」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 detain/iptools 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 detain/iptools 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 detain/iptools 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A stand-alone class implementation of the IPv4+IPv6 IP+CIDR aggregator from CIDRAM.
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
Manages IPv4 and IPv6 addresses and subnets
Official PHP SDK for the IPGeolocation.io IP Location API with single and bulk lookup support.
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
PHP SDK for Bands In Town API
统计信息
- 总下载量: 7.88k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-04-17