English | Русский
The helpers allows you to manipulating network IP addresses (IPv4 and IPv6).
Project repository: https://github.com/cs-eliseev/helpers-ip
DEMO
$ip = IP::getRealIP();
if (IP::isIP($ip)) {
switch (true) {
case IP::isIPv4($ip):
break;
case IP::isIPv6($ip):
$ip = IP::removeSubnetMaskIPv6($ip);
$ip = IP::getRangeIPv6($ip);
$ip = IP::getFirstIPByVersion($ip);
break;
}
$version = IP::getVersionIP($ip);
}CSE HELPERS is a collection of several libraries with simple functions written in PHP for people.
Despite using PHP as the main programming language for the Internet, its functions are not enough. IP CSE HELPERS for manipulating network IP addresses (IPv4 and IPv6).
CSE HELPERS was created for the rapid development of web applications.
CSE Helpers project:
- Array CSE helpers
- Cookie CSE helpers
- Date CSE helpers
- Email CSE helpers
- IP CSE helpers
- Json CSE helpers
- Math Converter CSE helpers
- Phone CSE helpers
- Request CSE helpers
- Session CSE helpers
- Word CSE helpers
Below you will find some information on how to init library and perform common commands.
You can find the most recent version of this project here.
Execute the following command to get the latest version of the package:
composer require cse/helpers-ipOr file composer.json should include the following contents:
{
"require": {
"cse/helpers-ip": "*"
}
}Clone this repository locally:
git clone https://github.com/cs-eliseev/helpers-ip.gitDownload the latest release here.
The class consists of static methods that are conveniently used in any project. See example examples-ip.php.
Get real IP address
Example:
IP::getRealIP();
// xxx.xxx.xxx.xxxRemove subnet mask IPv6
Example:
IP::removeSubnetMaskIPv6('2a0a:2b40::4:60/124');
// 2a0a:2b40::4:60Check is IPv4 address
Example:
IP::isIPv4('127.0.0.1');
// trueCheck is IPv6:
IP::isIPv4('2a0a:2b40::4:60');
// falseCheck is not IP:
IP::isIPv4('256.256.256');
// falseCheck is IPv6 address
Example:
IP::isIPv6('2a0a:2b40::4:60');
// trueCheck null:
IP::isIPv6('::');
// trueCheck localhost IPv6:
IP::isIPv6('::1');
// true
IP::isIPv6('0:0:0:0:0:0:0:1');
// trueCheck is IPv4:
IP::isIPv6('127.0.0.1');
// falseCheck is not IP:
IP::isIPv6(':');
// falseGet version IP address
Example:
IP::getVersionIP('::1');
// 6Get versin IPv4:
IP::getVersionIP('127.0.0.1');
// 4Get versin IPv6:
IP::getVersionIP('0:0:0:0:0:0:0:1');
// 6Get versin in not IP:
IP::getVersionIP('256.256.256.256');
// nullIs IP address
Example:
IP::isIP('::1');
// trueCheck versin IPv4:
IP::isIP('127.0.0.1');
// trueCheck versin IPv6:
IP::isIP('0:0:0:0:0:0:0:1');
// trueCheck versin in not IP:
IP::isIP('256.256.256.256');
// falseGet range IPv6 address
Example:
IP::getRangeIPv6('2a0a:2b40::4:60/124');
// ['2a0a:2b40::4:60', '2a0a:2b40::4:6f']Filter IPs address
Example:
IP::filterIPs([
'127.0.0.1',
'2a0a:2b40::4:60',
'255.255.255.255',
'2a0a:2b40::4:6f',
'256.256.256.256'
]);
// [4 => ['127.0.0.1', '255.255.255.255'], 6 => ['2a0a:2b40::4:60', '2a0a:2b40::4:6f']]Not data IP version:
IP::filterIPs([
'127.0.0.1',
'255.255.255.255',
'256.256.256.256'
]);
// [4 => ['127.0.0.1', '255.255.255.255'], 6 => []]Filter IPv4 address:
IP::filterIPs([
'127.0.0.1',
'2a0a:2b40::4:60',
'255.255.255.255',
'2a0a:2b40::4:6f',
'256.256.256.256'
], 4);
// ['127.0.0.1', '255.255.255.255']Filter IPv6 address:
IP::filterIPs([
'127.0.0.1',
'2a0a:2b40::4:60',
'255.255.255.255',
'2a0a:2b40::4:6f',
'256.256.256.256'
], 6);
// ['2a0a:2b40::4:60', '2a0a:2b40::4:6f']Get first IP by version
Example:
IP::getFirstIPByVersion([
'256.256.256.256',
'127.0.0.1',
'2a0a:2b40::4:60',
'255.255.255.255',
'2a0a:2b40::4:6f',
'256.256.256.256'
], 4);
// '127.0.0.1'Get first IP by version 6:
IP::getFirstIPByVersion([
'256.256.256.256',
'127.0.0.1',
'2a0a:2b40::4:60',
'255.255.255.255',
'2a0a:2b40::4:6f'
], 6);
// '2a0a:2b40::4:60'PHPUnit is used for unit testing. Unit tests ensure that class and methods does exactly what it is meant to do.
General PHPUnit documentation can be found at https://phpunit.de/documentation.html.
To run the PHPUnit unit tests, execute:
phpunit PATH/TO/PROJECT/tests/If you want code coverage reports, use the following:
phpunit --coverage-html ./report PATH/TO/PROJECT/tests/Used PHPUnit default config:
phpunit --configuration PATH/TO/PROJECT/phpunit.xmlYou can support this project here. You can also help out by contributing to the project, or reporting bugs. Even voicing your suggestions for features is great. Anything to help is much appreciated.
The IP CSE HELPERS is open-source PHP library licensed under the MIT license. Please see License File for more information.
GitHub @cs-eliseev