| 1 |
[](https://packagist.org/packages/wikimedia/ip-utils) |
| 2 |
|
| 3 |
IPUtils |
| 4 |
======= |
| 5 |
|
| 6 |
Parse, match, and analyze IP addresses and CIDR ranges. This library supports both IPv4 and IPv6. |
| 7 |
|
| 8 |
Additional documentation about the library can be found on |
| 9 |
[mediawiki.org](https://www.mediawiki.org/wiki/IPUtils). |
| 10 |
|
| 11 |
|
| 12 |
Usage |
| 13 |
----- |
| 14 |
|
| 15 |
<pre lang="php"> |
| 16 |
use Wikimedia\IPUtils; |
| 17 |
|
| 18 |
IPUtils::isIPAddress( '::1' ); |
| 19 |
IPUtils::isIPv4( '124.24.52.13' ); |
| 20 |
</pre> |
| 21 |
|
| 22 |
`IPSet` can be up to 100x faster than calling `IPUtils::isInRange()` |
| 23 |
over multiple CIDR specs. |
| 24 |
|
| 25 |
<pre lang="php"> |
| 26 |
use Wikimedia\IPSet; |
| 27 |
|
| 28 |
// This will calculate an optimized data structure for the set |
| 29 |
$ipset = new IPSet( [ |
| 30 |
'208.80.154.0/26', |
| 31 |
'2620:0:861:1::/64', |
| 32 |
'10.64.0.0/22', |
| 33 |
] ); |
| 34 |
|
| 35 |
// Run fast checks against the same re-usable IPSet object |
| 36 |
if ( $ipset->match( $ip ) ) { |
| 37 |
// ... |
| 38 |
} |
| 39 |
</pre> |
| 40 |
|
| 41 |
|
| 42 |
Running tests |
| 43 |
------------- |
| 44 |
|
| 45 |
composer install --prefer-dist |
| 46 |
composer test |
| 47 |
|
| 48 |
|
| 49 |
History |
| 50 |
------- |
| 51 |
|
| 52 |
The IPUtils class started life in 2006 as part of [MediaWiki 1.7][https://www.mediawiki.org/wiki/MediaWiki_1.7] ([r15572][https://www.mediawiki.org/wiki/Special:Code/MediaWiki/15572]). It was |
| 53 |
split out of the MediaWiki codebase and published as an independent library |
| 54 |
during the [MediaWiki 1.34][https://www.mediawiki.org/wiki/MediaWiki_1.34] development cycle. |
| 55 |
|
| 56 |
The IPSet class was created by Brandon Black in 2014 as faster alternative to `IPUtils::isInRange()` (MediaWiki 1.24, [change 131758](https://gerrit.wikimedia.org/r/131758)). It was moved to a library during the MediaWiki 1.26 development cycle ([change 221179](https://gerrit.wikimedia.org/r/221179), [change 218384](https://gerrit.wikimedia.org/r/218384)). |