C extension for PHP - IPv6 Library
- PHP 5.2 or higher
- Make sure you've phpize and php-config installed.
- cd ipv6-php-extension
- /path/to/phpize
- ./configure --with-php-config=/path/to/php-config --enable-libipv6
- make
- make test # optional
- sudo make install
Add extension=libipv6.so to your php.ini
Test the code by running /path/to/php -d"extension=libipv6.so" tests/test.php
This extension will provide following functions.
<?php
$ip = 'fe80::226:2dff:fefa:dcba';
if (is_valid_ipv6($ip)) {
echo "This is valid IPv6 Address\n";
}
Full expanded IPv6 address
<?php
$ip = 'fe80::226:2dff:fefa:dcba';
echo get_full_ipv6($ip);
// prints: fe80:0000:0000:0000::0226:2dff:fefa:dcba
Full expanded IPv6 address
<?php
$ip = 'fe80:0000:0000:0000::0226:2dff:fefa:dcba';
echo get_short_ipv6($ip);
// prints: fe80::226:2dff:fefa:dcba
<?php
$ip = 'fe80::226:2dff:fefa:dcba';
echo get_next_ipv6($ip);
// prints: fe80::226:2dff:fefa:dcbb
<?php
$ip = 'fe80::226:2dff:fefa:dcbb';
echo get_prev_ipv6($ip);
// prints: fe80::226:2dff:fefa:dcba
Return any one of following things.
Return value | Description |
---|---|
-1 | First is less than second |
0 | both are equal |
1 | First is greater than second |
<?php
$first = 'fe80::226:2dff:fefa:dcba';
$second = 'fe80::226:2dff:fefa:dcbb';
echo compare_ipv6($first, $second);
// prints: -1
$first = 'fe80::226:2dff:fefa:dcbf';
$second = 'fe80::226:2dff:fefa:dcba';
echo compare_ipv6($first, $second);
// prints: 1
$first = 'fe80::226:2dff:fefa:dcba';
$second = 'fe80::226:2dff:fefa:dcba';
echo compare_ipv6($first, $second);
// prints: 0
get first N common bits (it should be in the range 0 to 128 (both inclusive))
<?php
$first = 'fe80::226:2dff:fefa:dcb1';
$second = 'fe80::226:2dff:fefa:dcb2';
// they have first 126 bits in common
echo get_common_bits($first, $second);
// prints: 126
Check given network ia valid.
<?php
$ip = '2001:db8:fe18::/48';
if (is_valid_ipv6_network($ip)) {
echo "This is valid IPv6 Network\n";
}
is IPv4-Compatible and IPv4-Mapped Embedded IPv6 Addresses.
it would be represented as 0:0:0:0:0:FFFF:222.1.41.90, or ::FFFF:222.1.41.90.
<?php
$ip = '::FFFF:222.1.41.90';
if (is_ipv4_mapped_ipv6($ip)) {
echo "This is valid IPv4-Mapped Embedded IPv6 Address\n";
}
This extension will provide following classes.
<?php
$ip = IPv6Address::fromString('fe80::226:2dff:fefa:dcb1');
echo $ip->__toString();
echo $ip->getFullAddress();
Signature | Description |
---|---|
public static fromString( string $ipString ) |
Return: IPv6Address Create IPv6Address object from string |
public __toString() |
Return: string Get short form of IPv6 string |
public getFullAddress() |
Return: string Get full/expanded form of IPv6 string |
public nextAddress( $asString = false ) |
Return: IPv6Address by defaultReturn: string when $asString is trueGet next address |
public prevAddress( $asString = false ) |
Return: IPv6Address by defaultReturn: string when $asString is trueget previous address |
public `compare( IPv6Address | string $other )` |
public `commonBits( IPv6Address | string $other )` |
public add( int $number ) |
Return: IPv6Address or null Add to this address and return the result IPv6Address object |
public subtract( $number ) |
Return: IPv6Address or null Substract from this address and return the result IPv6Address object |
public `maskWithNetworkMask( IPv6NetworkMask | int $mask, bool $returnString=false )` |
<?php
$range = IPv6Range::fromString('fe80::226:2dff:fefa:dcb1', 'fe80::226:2dff:fefa:ffff');
echo $range->__toString();
Signature | Description |
---|---|
public static fromString( string $ipStart, string $ipEnd ) |
Return: IPv6Address create IPv6Range from start and end |
public __toString() |
Return: string Get string of IPv6 range |
public `containsAddress( IPv6Address | string $address )` |
public containsRange( IPv6Range $range ) |
Return: bool Check other range exist inside this range |
public getMinAddress($returnString = false) |
Return: IPv6Address or string if $returnString is truereturn Start address of this range or network. |
public getMaxAddress($returnString = false) |
Return: IPv6Address or string if $returnString is truereturn End address of this range or network. |
public static merge( IPv6Range $range1, IPv6Range $range2 ) |
Return: IPv6Range return merged Range, or return null if they are completly disjoint. |
public static intersect( IPv6Range $range1, IPv6Range $range2 ) |
Return: IPv6Range return intersected Range or null if there no intersection. |
All methods of IPv6Range are available here because IPv6Network extends IPv6Range
<?php
$network = IPv6Network::fromString('fe80::226:2dff::/48');
echo $network->__toString();
Signature | Description |
---|---|
public static fromString( string $ipNetwork ) |
Return: IPv6Network Create IPv6Network object from network string |
All methods of IPv6Network are available here becuase IPv6NetworkMask extends IPv6Network
<?php
$networkMask = IPv6NetworkMask::fromPrefixLength(40);
echo $networkMask->__toString();
// ffff:ffff:ff00::/40
Signature | Description |
---|---|
public static fromPrefixLength( int $prefix ) |
Return: IPv6NetworkMask Create IPv6NetworkMask from number of bits (1 to 128 ) |