php 000000000000,该PHP库提供用于处理最常见网络地址的类型

这篇博客介绍了一个PHP库,用于处理和比较各种网络地址类型,包括MAC、IPv4、IPv6和CIDR地址。该库支持从字符串构造地址,并能进行正常化、序列化为JSON,以及进行地址和网络的比较。它还提供了将地址转换为字节序列的功能,方便在数据库中存储。通过`NetworkAddress`类和其子类,可以方便地操作和检查网络地址是否在特定网络范围内。
摘要由CSDN通过智能技术生成

Network Address Types for PHP

68747470733a2f2f7472617669732d63692e6f72672f6c756b616e6574636f6e73756c742f6e6574776f726b2d616464726573732d74797065732e7376673f6272616e63683d6d617374657268747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f30643463666533366366353761353032626238642f746573745f636f76657261676568747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f30643463666533366366353761353032626238642f6d61696e7461696e6162696c69747968747470733a2f2f62616467652e737472796b65722d6d757461746f722e696f2f6769746875622e636f6d2f6c756b616e6574636f6e73756c742f6e6574776f726b2d616464726573732d74797065732f6d6173746572

This library provides types to handle the most common network addresses. The following address types are supported:

MAC addresses in the following forms:

Colon separated (00:00:00:00:00:00)

Dash separated (00-00-00-00-00-00)

Without separator (000000000000)

IPv4 (ex: 127.0.0.1)

IPv6 (ex: ::1)

CIDR addresses for IPv4 and IPv6 (ex: 129.0.0.1/12, ff80:e::/64)

Installation

Installation can be performed with composer:

composer require luka/network-address-types

Requirements

PHP >= 7.4

ext-gmp for handling IPv6 calculations

ext-json as it supports json_encode by implementing JSONSerializable

Getting started

use LUKA\Network\NetworkAddress;

$address = NetworkAddress::fromString('127.0.0.1');

assert($address instanceof \LUKA\Network\IPv4\IPv4Address);

$address = NetworkAddress::fromString('127.0.0.1/8');

assert($address instanceof \LUKA\Network\IPv4\CIDRv4Address);

$address = NetworkAddress::fromString('::1');

assert($address instanceof \LUKA\Network\IPv6\IPv6Address);

$address = NetworkAddress::fromString('ff80::1/64');

assert($address instanceof \LUKA\Network\IPv6\CIDRv6Address);

$address = NetworkAddress::fromString('84:34:ff:ff:ff:ff');

assert($address instanceof \LUKA\Network\MACAddress);

Serialization

All types can be constructed from strings and can therefore be converted to strings as well with the toString() method.

use LUKA\Network\NetworkAddress;

assert('::1' === NetworkAddress::fromString('::1')->toString());

When converting addresses to strings, they will be normalized for the corresponding type:

use LUKA\Network\NetworkAddress;

assert('::1' === NetworkAddress::fromString('0:0:0:0::1')->toString());

assert('00:00:00:00:00:00' === NetworkAddress::fromString('00-00-00-00-00-00')->toString());

JSON

All address types implement the JSONSerializable interface, and can therefore be used with json_encode() directly.

Binary

IP and MAC addresses can also be converted to the corresponding byte sequence. (for example to storing them in a BINARY database field).

They can also be constructed from this byte sequence with the static fromByteString() method of the corresponding class.

Address Comparison

Compare equality

Each address implements an equals() method to compare it to other network addresses.

Addresses are considered equal when they are from the same type and contain the same value:

use LUKA\Network\NetworkAddress;

assert(

true === NetworkAddress::fromString('::1')

->equals(NetworkAddress::fromString('::1'))

);

// Value mismatch:

assert(

false === NetworkAddress::fromString('::1')

->equals(NetworkAddress::fromString('::2'))

);

// Type mismatch (different IP version):

assert(

false === NetworkAddress::fromString('::1')

->equals(NetworkAddress::fromString('127.0.0.1'))

);

// Type mismatch (cidr vs non-cidr)

assert(

false === NetworkAddress::fromString('192.168.0.5')

->equals(NetworkAddress::fromString('192.168.0.5/24'))

);

Comparing addresses to networks

CIDR addresses allow to obtain the corresponding network to the denoted address. With this network you can check if an IP address is within this network.

use LUKA\Network\NetworkAddress;

$cidr = NetworkAddress::fromString('192.168.0.7/8');

$network = $cidr->toNetwork();

assert(true === $network->containsAddress(NetworkAddress::fromString('192.168.0.1')));

assert(true === $network->containsAddress(NetworkAddress::fromString('192.45.0.2')));

assert(false === $network->containsAddress(NetworkAddress::fromString('127.10.0.1')));

assert(false === $network->containsAddress(NetworkAddress::fromString('ff80::5')));

This will work for IPv6 as well:

use LUKA\Network\NetworkAddress;

$cidr = NetworkAddress::fromString('ff80::/64');

$network = $cidr->toNetwork();

assert(true === $network->containsAddress(NetworkAddress::fromString('ff80::1')));

assert(true === $network->containsAddress(NetworkAddress::fromString('ff80::10:e5:7')));

assert(false === $network->containsAddress(NetworkAddress::fromString('ff80:e::1')));

assert(false === $network->containsAddress(NetworkAddress::fromString('127.0.0.1')));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值