https://github.com/matomo-org/component-network
Network component: manipulate IP addresses (ipv4, ipv6) in PHP used in the Matomo project
https://github.com/matomo-org/component-network
ip matomo network php
Last synced: 3 months ago
JSON representation
Network component: manipulate IP addresses (ipv4, ipv6) in PHP used in the Matomo project
- Host: GitHub
- URL: https://github.com/matomo-org/component-network
- Owner: matomo-org
- License: lgpl-3.0
- Created: 2014-10-15T01:01:08.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2025-10-08T15:01:12.000Z (3 months ago)
- Last Synced: 2025-10-08T17:23:22.209Z (3 months ago)
- Topics: ip, matomo, network, php
- Language: PHP
- Homepage:
- Size: 125 KB
- Stars: 21
- Watchers: 16
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Matomo/Network
Component providing Network tools.
[](https://github.com/matomo-org/component-network/actions/workflows/phpunit.yml)
## Installation
With Composer:
```json
{
"require": {
"matomo/network": "*"
}
}
```
## Usage
### IP
To manipulate an IP address, you can use the `Matomo\Network\IP` class:
```php
$ip = IP::fromStringIP('127.0.0.1');
// IPv6
$ip = IP::fromStringIP('::1');
// In binary format:
$ip = IP::fromBinaryIP("\x7F\x00\x00\x01");
echo $ip->toString(); // 127.0.0.1
echo $ip->toBinary();
// IPv4 & IPv6
if ($ip instanceof IPv4) {}
if ($ip instanceof IPv6) {}
// Hostname reverse lookup
echo $ip->getHostname();
if ($ip->isInRange('192.168.1.1/32')) {}
if ($ip->isInRange('192.168.*.*')) {}
// Anonymize an IP by setting X bytes to null bytes
$ip->anonymize(2);
```
The `Matomo\Network\IPUtils` class provides utility methods:
```php
echo IPUtils::binaryToStringIP("\x7F\x00\x00\x01");
echo IPUtils::stringToBinaryIP('127.0.0.1');
// Sanitization methods
$sanitizedIp = IPUtils::sanitizeIp($_GET['ip']);
$sanitizedIpRange = IPUtils::sanitizeIpRange($_GET['ipRange']);
// IP range
$bounds = IPUtils::getIPRangeBounds('192.168.1.*');
echo $bounds[0]; // 192.168.1.0
echo $bounds[1]; // 192.168.1.255
```
## License
The Network component is released under the [LGPL v3.0](http://choosealicense.com/licenses/lgpl-3.0/).