Giter Site home page Giter Site logo

cs-eliseev / helpers-ip Goto Github PK

View Code? Open in Web Editor NEW
5.0 4.0 2.0 59 KB

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

License: MIT License

PHP 100.00%
php cse ip ips ipv4 ipv6 ips4 collection collections tool tools utilities utils utility helper helpers library framework network networking

helpers-ip's Introduction

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

helpers-ip's People

Contributors

cs-eliseev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.