1. Go to this page and download the library: Download mlocati/ip-lib library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$address = \IPLib\Factory::parseAddressString('::1');
// This will print ::
echo (string) $address->getPreviousAddress();
// This will print ::2
echo (string) $address->getNextAddress();
$address = \IPLib\Factory::parseAddressString('::1');
// This will print ::1
echo (string) $address->getAddressAtOffset(0);
// This will print ::2
echo (string) $address->getAddressAtOffset(1);
// This will print ::3
echo (string) $address->getAddressAtOffset(2);
// This will print ::3e9
echo (string) $address->getAddressAtOffset(1000);
// This will print ::
echo (string) $address->getAddressAtOffset(-1);
// This will print NULL
echo var_dump($address->getAddressAtOffset(-2));
$range = \IPLib\Factory::parseRangeString('::ff00/120');
// This will print ::ff00
echo (string) $range->getAddressAtOffset(0);
// This will print ::ff10
echo (string) $range->getAddressAtOffset(16);
// This will print ::ff64
echo (string) $range->getAddressAtOffset(100);
// This will print NULL because the address ::1:0 is out of the range
var_dump($range->getAddressAtOffset(256));
// This will print ::ffff
echo (string) $range->getAddressAtOffset(-1);
// This will print ::fff0
echo (string) $range->getAddressAtOffset(-16);
// This will print ::ff00
echo (string) $range->getAddressAtOffset(-256);
// This will print NULL because the address ::feff is out of the range
var_dump($range->getAddressAtOffset(-257));
$range = \IPLib\Factory::getRangeFromBoundaries('192.168.0.1', '192.168.255.255');
// This will print 192.168.0.0/16
echo (string) $range;
$ranges = \IPLib\Factory::getRangesFromBoundaries('192.168.0.0', '192.168.0.5');
// This will print 192.168.0.0/30 192.168.0.4/31
echo implode(' ', $ranges);
$range = \IPLib\Factory::parseRangeString('127.0.0.*');
// This will print 127.0.0.0
echo (string) $range->getStartAddress();
// This will print 127.0.0.255
echo (string) $range->getEndAddress();
// This will print 127.0.0.1
echo \IPLib\Factory::parseAddressString('127.0.0.1')->toString();
// This will print 127.0.0.1
echo \IPLib\Factory::parseAddressString('127.000.000.001')->toString();
// This will print ::1
echo \IPLib\Factory::parseAddressString('::1')->toString();
// This will print ::1
echo \IPLib\Factory::parseAddressString('0:0::1')->toString();
// This will print ::1/64
echo \IPLib\Factory::parseRangeString('0:0::1/64')->toString();
// This will print 0000:0000:0000:0000:0000:0000:0000:0000
echo \IPLib\Factory::parseAddressString('::')->toString(true);
// This will print 0000:0000:0000:0000:0000:0000:0000:0001
echo \IPLib\Factory::parseAddressString('::1')->toString(true);
// This will print 0fff:0000:0000:0000:0000:0000:0000:0000
echo \IPLib\Factory::parseAddressString('fff::')->toString(true);
// This will print 0000:0000:0000:0000:0000:0000:0000:0000
echo \IPLib\Factory::parseAddressString('::0:0')->toString(true);
// This will print 0001:0002:0003:0004:0005:0006:0007:0008
echo \IPLib\Factory::parseAddressString('1:2:3:4:5:6:7:8')->toString(true);
// This will print 0000:0000:0000:0000:0000:0000:0000:0001/64
echo \IPLib\Factory::parseRangeString('0:0::1/64')->toString();
$address = \IPLib\Address\IPv6::parseString('::1');
// All these will print ::1
echo $address->toString();
echo $address->toString(false);
echo (string) $address;
$range = \IPLib\Factory::parseRangeString('2000:0::1/64');
// $type will contain the value of \IPLib\Range\Type::T_PUBLIC
$type = $range->getRangeType();
// This will print Public address
echo \IPLib\Range\Type::getName($type);
$range = \IPLib\Factory::parseRangeString('::/127');
// $type will contain null
$type = $range->getRangeType();
// This will print Unknown type
echo \IPLib\Range\Type::getName($type);
$ipv4 = \IPLib\Factory::parseAddressString('1.2.3.4');
// 6to4 notation
$ipv6 = $ipv4->toIPv6();
// This will print 2002:102:304::
echo (string) $ipv6;
// This will print 1.2.3.4
echo $ipv6->toIPv4();
// IPv4-mapped notation
$ipv6_6to4 = $ipv4->toIPv6IPv4Mapped();
// This will print ::ffff:1.2.3.4
echo (string) $ipv6_6to4;
// This will print 1.2.3.4
echo $ipv6_6to4->toIPv4();
// This will print ::*:*:*:*
echo \IPLib\Factory::parseRangeString('::/64')->asPattern()->toString();
// This will print 1:2::/96
echo \IPLib\Factory::parseRangeString('1:2::*:*')->asSubnet()->toString();
// This will print 192.168.0.0/24
echo \IPLib\Factory::parseRangeString('192.168.0.*')->asSubnet()->toString();
// This will print 10.*.*.*
echo \IPLib\Factory::parseRangeString('10.0.0.0/8')->asPattern()->toString();
// This will print 255.255.255.0
echo \IPLib\Factory::parseRangeString('192.168.0.*')->getSubnetMask()->toString();
// This will print 255.255.255.252
echo \IPLib\Factory::parseRangeString('192.168.0.12/30')->getSubnetMask()->toString();
// This will print 256
echo \IPLib\Factory::parseRangeString('192.168.0.*')->getSize();
// This will print 4
echo \IPLib\Factory::parseRangeString('192.168.0.12/30')->getSize();
// This will print 1
echo \IPLib\Factory::parseRangeString('192.168.0.1')->getSize();
$ipv4 = \IPLib\Factory::parseAddressString('1.2.3.255');
$ipv6 = \IPLib\Factory::parseAddressString('1234:abcd::cafe:babe');
// This will print 255.3.2.1.in-addr.arpa
echo $ipv4->getReverseDNSLookupName();
// This will print e.b.a.b.e.f.a.c.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.d.c.b.a.4.3.2.1.ip6.arpa
echo $ipv6->getReverseDNSLookupName();
$ipv4 = \IPLib\Factory::parseAddressString('255.3.2.1.in-addr.arpa', \IPLib\ParseStringFlag::ADDRESS_MAYBE_RDNS);
$ipv6 = \IPLib\Factory::parseAddressString('e.b.a.b.e.f.a.c.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.d.c.b.a.4.3.2.1.ip6.arpa', \IPLib\ParseStringFlag::ADDRESS_MAYBE_RDNS);
// This will print 1.2.3.255
echo $ipv4->toString();
// This will print 1234:abcd::cafe:babe
echo $ipv6->toString();
/*
* Let's assume that:
* - $pdo is a PDO instance
* - $range is a range object
* - $address is an address object
*/
// Save the $range object
$insertQuery = $pdo->prepare('
insert into ranges (addressType, rangeFrom, rangeTo)
values (:addressType, :rangeFrom, :rangeTo)
');
$insertQuery->execute(array(
':addressType' => $range->getAddressType(),
':rangeFrom' => $range->getComparableStartString(),
':rangeTo' => $range->getComparableEndString(),
));
// Retrieve the saved ranges where an address $address falls:
$searchQuery = $pdo->prepare('
select * from ranges
where addressType = :addressType
and :address between rangeFrom and rangeTo
');
$searchQuery->execute(array(
':addressType' => $address->getAddressType(),
':address' => $address->getComparableString(),
));
$rows = $searchQuery->fetchAll();
$searchQuery->closeCursor();
use IPLib\Factory;
use IPLib\ParseStringFlag;
parseAddressString('127.0.0.1:80'));
var_export(Factory::parseAddressString('[::]:80'));
// This will print 127.0.0.1
echo (string) Factory::parseAddressString('127.0.0.1:80', ParseStringFlag::MAY_INCLUDE_PORT);
// This will print ::
echo (string) Factory::parseAddressString('[::]:80', ParseStringFlag::MAY_INCLUDE_PORT);
use IPLib\Factory;
use IPLib\ParseStringFlag;
// This will print NULL
var_export(Factory::parseAddressString('::%11'));
// This will print ::
echo (string) Factory::parseAddressString('::%11', ParseStringFlag::MAY_INCLUDE_ZONEID);
use IPLib\Factory;
use IPLib\ParseStringFlag;
// This will print NULL
var_export(Factory::parseAddressString('0177.0.0.0x1'));
// This will print 127.0.0.1
var_export((string) Factory::parseAddressString('0177.0.0.0x1', ParseStringFlag::IPV4_MAYBE_NON_DECIMAL));
// This will print NULL
var_export(Factory::parseRangeString('0177.0.0.0x1/32'));
// This will print 127.0.0.1/32
var_export((string) Factory::parseRangeString('0177.0.0.0x1/32', ParseStringFlag::IPV4_MAYBE_NON_DECIMAL));
use IPLib\Factory;
use IPLib\ParseStringFlag;
// This will print 127.0.0.10 since the last digit is assumed to be decimal
var_export((string) Factory::parseAddressString('127.0.0.010'));
// This will print 127.0.0.8 since the last digit is assumed to be octal
var_export((string) Factory::parseAddressString('127.0.0.010', ParseStringFlag::IPV4_MAYBE_NON_DECIMAL));
use IPLib\Factory;
use IPLib\ParseStringFlag;
// This will print NULL
var_export(Factory::parseAddressString('1.2.500'));
// This will print 0.0.0.0
var_export((string) Factory::parseAddressString('0', ParseStringFlag::IPV4ADDRESS_MAYBE_NON_QUAD_DOTTED));
// This will print 0.0.0.1
var_export((string) Factory::parseAddressString('1', ParseStringFlag::IPV4ADDRESS_MAYBE_NON_QUAD_DOTTED));
// This will print 0.0.1.244
var_export((string) Factory::parseAddressString('0.0.500', ParseStringFlag::IPV4ADDRESS_MAYBE_NON_QUAD_DOTTED));
// This will print 255.255.255.255
var_export((string) Factory::parseAddressString('4294967295', ParseStringFlag::IPV4ADDRESS_MAYBE_NON_QUAD_DOTTED));
use IPLib\Factory;
use IPLib\ParseStringFlag;
// This will print NULL
var_export(Factory::parseRangeString('127/24'));
// This will print 127.0.0.0/24
echo (string) Factory::parseRangeString('127/24', ParseStringFlag::IPV4SUBNET_MAYBE_COMPACT);
use IPLib\Factory;
use IPLib\ParseStringFlag;
// This will print 127.0.0.255
var_export((string) Factory::parseAddressString('127.0.0.0xff:80', ParseStringFlag::MAY_INCLUDE_PORT | ParseStringFlag::IPV4_MAYBE_NON_DECIMAL));
// This will print ::
var_export((string) Factory::parseAddressString('[::%11]:80', ParseStringFlag::MAY_INCLUDE_PORT | ParseStringFlag::MAY_INCLUDE_ZONEID));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.