Download the PHP package mlocati/ip-lib without Composer

On this page you can find all versions of the php package mlocati/ip-lib. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package ip-lib

Tests Coverage Status Scrutinizer Code Quality Packagist Downloads Open in Gitpod

IPLib - Handle IPv4, IPv6 and IP ranges

Introduction

IPLib is a modern, PSR-compliant, test-driven IP addresses and subnets manipulation library. It implements primitives to handle IPv4 and IPv6 addresses, as well as IP ranges (subnets), in CIDR format (like ::1/128 or 127.0.0.1/32) and in pattern format (like ::*:* or 127.0.*.*).

Requirements

IPLib has very basic requirements as:

Installation

Manual installation

Download the latest version, unzip it and add these lines in our PHP files:

Installation with Composer

Simply run

or add these lines to your composer.json file:

Sample usage

Parse an address

To parse an IPv4 address:

To parse an IPv6 address:

To parse an address in any format (IPv4 or IPv6):

Get the next/previous addresses

Get the addresses at a specified offset

For addresses:

For ranges:

Parse an IP address range

To parse a subnet (CIDR) range:

To parse a pattern (asterisk notation) range:

To parse an address as a range:

To parse a range in any format:

Retrieve a range from its boundaries

You can calculate the smallest range that comprises two addresses:

You can also calculate a list of ranges that exactly describes all the addresses between two addresses:

Retrieve the boundaries of a range

Format addresses and ranges

Both IP addresses and ranges have a toString method that you can use to retrieve a textual representation:

When working with IPv6, you may want the full (expanded) representation of the addresses. In this case, simply use a true parameter for the toString method:

The address and range objects implements the __toString() method, which call the toString() method. So, if you want the string (short) representation of an object, you can do any of the following:

Check if an address is contained in a range

All the range types offer a contains method, and all the IP address types offer a matches method: you can call them to check if an address is contained in a range:

Please remark that if the address is IPv4 and the range is IPv6 (or vice-versa), the result will always be false.

Check if a range contains another range

All the range types offer a containsRange method: you can call them to check if an address range fully contains another range:

Getting the type of an IP address

If you want to know if an address is within a private network, or if it's a public IP, or whatever you want, you can use the getRangeType method:

The most notable values of the range type are:

Getting the type of an IP address range

If you want to know the type of an address range, you can use the getRangeType method:

Please note that if a range spans across multiple range types, you'll get NULL as the range type:

Converting IP addresses

This library supports converting IPv4 to/from IPv6 addresses using the 6to4 notation or the IPv4-mapped notation:

Converting IP ranges

This library supports IPv4/IPv6 ranges in pattern format (eg. 192.168.*.*) and in CIDR/subnet format (eg. 192.168.0.0/16), and it offers a way to convert between the two formats:

Please remark that all the range types implement the asPattern() and asSubnet() methods.

Getting the subnet mask for IPv4 ranges

You can use the getSubnetMask() to get the subnet mask for IPv4 ranges:

Getting the range size

You can use the getSize() to get the count of addresses this IP range contains:

Getting the reverse DNS lookup address

To perform reverse DNS queries, you need to use a special format of the IP addresses.

You can use the getReverseDNSLookupName() method of the IP address instances to retrieve it easily:

To parse addresses in reverse DNS lookup format you can use the IPLib\ParseStringFlag::ADDRESS_MAYBE_RDNS flag when parsing a string:

You can also use getReverseDNSLookupName() for IP ranges. In this case, the result is an array of strings:

Using a database

This package offers a great feature: you can store address ranges in a database table, and check if an address is contained in one of the saved ranges with a simple query.

To save a range, you need to store the address type (for IPv4 it's 4, for IPv6 it's 6), as well as two values representing the start and the end of the range. These methods are:

Let's assume that you saved the type in a field called addressType, and the range boundaries in two fields called rangeFrom and rangeTo.

When you want to check if an address is within a stored range, simply use the getComparableString method of the address and check if it's between the fields rangeFrom and rangeTo, and check if the stored addressType is the same as the one of the address instance you want to check.

Here's a sample code:

Handling non-standard address and range strings

Accepting ports

If you want to accept addresses that may include ports, you can specify the IPLib\ParseStringFlag::MAY_INCLUDE_PORT flag:

Accepting IPv6 zone IDs

If you want to accept IPv6 addresses that may include a zone ID, you can specify the IPLib\ParseStringFlag::MAY_INCLUDE_ZONEID flag:

Accepting non-decimal IPv4 addresses

IPv4 addresses are usually expressed in decimal notation, for example as 192.168.0.1.

By the way, the GNU (used in many Linux distros), BSD (used in Mac) and Windows implementations of inet_aton and inet_addr accept IPv4 addresses with numbers in octal and/or hexadecimal format. Please remark that this does not apply to the inet_pton and ip2long functions, as well as to the Musl implementation (used in Alpine Linux) of inet_aton and inet_addr.

So, for example, these addresses are all equivalent to 192.168.0.1:

(try it: if you browse to http://0177.0.0.0x1, your browser will try to browse http://127.0.0.1).

If you want to accept this non-decimal syntax, you may use the IPLib\ParseStringFlag::IPV4_MAYBE_NON_DECIMAL flag:

Please be aware that the IPV4_MAYBE_NON_DECIMAL flag may also affect parsing decimal numbers:

Accepting IPv4 addresses in not-quad-dotted notation

IPv4 addresses are usually expressed with 4 numbers, for example as 192.168.0.1.

By the way, the GNU (used in many Linux distros), BSD (used in Mac) and Windows implementations of inet_aton and inet_addr accept IPv4 addresses with 1 to 4 numbers.

Please remark that this does not apply to the inet_pton and ip2long functions, as well as to the Musl implementation (used in Alpine Linux) of inet_aton and inet_addr.

If you want to accept this non-decimal syntax, you may use the IPLib\ParseStringFlag::IPV4ADDRESS_MAYBE_NON_QUAD_DOTTED flag:

Accepting compact IPv4 subnet notation

Even if there isn't an RFC that describe it, IPv4 subnet notation may also be written in a compact form, omitting extra digits (for example, 127.0.0.0/24 may be written as 127/24). If you want to accept such format, you can specify the IPLib\ParseStringFlag::IPV4SUBNET_MAYBE_COMPACT flag:

Combining multiple flags

Of course, you may use more than one IPLib\ParseStringFlag flag at once:

Gitpod Environment Variables

The following features can be enabled through environment variables that have been set in your Gitpod preferences.:

* Please note that storing sensitive data in environment variables is not ultimately secure but should be OK for most development situations.

Do you really want to say thank you?

You can offer me a monthly coffee or a one-time coffee :wink:


All versions of ip-lib with dependencies

PHP Build Version
Package Version
Requires php Version >=5.3.3
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mlocati/ip-lib contains the following files

Loading the files please wait ....