cse/helpers-ip

The helpers allows you to manipulating network IP addresses (IPv4 and IPv6).

1.0.4 2019-03-08 05:38 UTC

This package is auto-updated.

Last update: 2024-11-29 04:52:28 UTC


README

English | Русский

IP CSE HELPERS

Travis (.org) Codecov Scrutinizer code quality

Packagist Minimum PHP Version Packagist GitHub repo size

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);
}

Introduction

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:

Below you will find some information on how to init library and perform common commands.

Install

You can find the most recent version of this project here.

Composer

Execute the following command to get the latest version of the package:

composer require cse/helpers-ip

Or file composer.json should include the following contents:

{
    "require": {
        "cse/helpers-ip": "*"
    }
}

Git

Clone this repository locally:

git clone https://github.com/cs-eliseev/helpers-ip.git

Download

Download the latest release here.

Usage

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.xxx

Remove subnet mask IPv6

Example:

IP::removeSubnetMaskIPv6('2a0a:2b40::4:60/124');
// 2a0a:2b40::4:60

Check is IPv4 address

Example:

IP::isIPv4('127.0.0.1');
// true

Check is IPv6:

IP::isIPv4('2a0a:2b40::4:60');
// false

Check is not IP:

IP::isIPv4('256.256.256');
// false

Check is IPv6 address

Example:

IP::isIPv6('2a0a:2b40::4:60');
// true

Check null:

IP::isIPv6('::');
// true

Check localhost IPv6:

IP::isIPv6('::1');
// true
IP::isIPv6('0:0:0:0:0:0:0:1');
// true

Check is IPv4:

IP::isIPv6('127.0.0.1');
// false

Check is not IP:

IP::isIPv6(':');
// false

Get version IP address

Example:

IP::getVersionIP('::1');
// 6

Get versin IPv4:

IP::getVersionIP('127.0.0.1');
// 4

Get versin IPv6:

IP::getVersionIP('0:0:0:0:0:0:0:1');
// 6

Get versin in not IP:

IP::getVersionIP('256.256.256.256');
// null

Is IP address

Example:

IP::isIP('::1');
// true

Check versin IPv4:

IP::isIP('127.0.0.1');
// true

Check versin IPv6:

IP::isIP('0:0:0:0:0:0:0:1');
// true

Check versin in not IP:

IP::isIP('256.256.256.256');
// false

Get 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'

Testing & Code Coverage

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.xml

Donating

You 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.

License

The IP CSE HELPERS is open-source PHP library licensed under the MIT license. Please see License File for more information.

GitHub @cs-eliseev