PHP code example of detain / iptools
1. Go to this page and download the library: Download detain/iptools 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/ */
detain / iptools example snippets
$ip = new IP('192.168.1.1');
echo $ip->version;// IPv4
$ip = new IP('fc00::');
echo $ip->version; // IPv6
echo (string)IP::parse(2130706433); // 127.0.0.1
echo (string)IP::parse('0b11000000101010000000000100000001') // 192.168.1.1
echo (string)IP::parse('0x0a000001'); // 10.0.0.1
echo (string)IP::parseLong(2130706433); // 127.0.0.1
echo (string)IP::parseBin('11000000101010000000000100000001'); // 192.168.1.1
echo (string)IP::parseHex('0a000001'); // 10.0.0.1
echo IP::parse('192.168.1.1')->bin // 11000000101010000000000100000001
echo IP::parse('10.0.0.1')->hex // 0a000001
echo IP::parse('127.0.0.1')->long // 2130706433
echo new IP::parse('192.0.2.5')->reversePointer // 5.2.0.192.in-addr.arpa
echo new IP::parse('2001:db8::567:89ab')->reversePointer // b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa
echo Network::parse('192.0.0.1 255.0.0.0')->CIDR; // 192.0.0.0/8
echo (string)Network::parse('192.0.0.1/8')->netmask; // 255.0.0.0
echo (string)Network::parse('192.0.0.1'); // 192.0.0.1/32
$excluded = Network::parse('192.0.0.0/8')->exclude(new IP('192.168.1.1'));
foreach($excluded as $network) {
echo (string)$network . '<br>';
}
$excluded = Network::parse('192.0.0.0/8')->exclude(new Network('192.168.1.0/24'));
foreach($excluded as $network) {
echo (string)$network . '<br>';
}
$excluded = Network::parse('192.0.0.0/8')->excludeArray(['192.168.1.0/24', '192.168.2.0/24']);
foreach($excluded as $network) {
echo (string)$network . '<br>';
}
$networks = Network::parse('192.168.0.0/22')->moveTo('24');
foreach ($networks as $network) {
echo (string)$network . '<br>';
}
$network = Network::parse('192.168.1.0/24');
foreach($network as $ip) {
echo (string)$ip . '<br>';
}
$hosts = Network::parse('192.168.1.0/24')->hosts // Range(192.168.1.1, 192.168.1.254);
foreach($hosts as $ip) {
echo (string)$ip . '<br>';
}
echo count(Network::parse('192.168.1.0/24')) // 254
$range = new Range(new IP('192.168.1.0'), new IP('192.168.1.255'));
$range = Range::parse('192.168.1.0-192.168.1.255');
$range = Range::parse('192.168.1.*');
$range = Range::parse('192.168.1.0/24');
echo Range::parse('192.168.1.1-192.168.1.254')->contains(new IP('192.168.1.5')); // true
echo Range::parse('::1-::ffff')->contains(new IP('::1234')); // true
echo Range::parse('192.168.1.1-192.168.1.254')->containsAny([new IP('192.168.0.5'), new IP('192.168.1.5')]); // true
echo Range::parse('192.168.1.1-192.168.1.254')->containsAll([new IP('192.168.0.5'), new IP('192.168.1.5')]); // false
echo Range::parse('192.168.1.1-192.168.1.254')->containsAll([new IP('192.168.1.6'), new IP('192.168.1.5')]); // true
$range = Range::parse('192.168.1.1-192.168.1.254');
foreach($range as $ip) {
echo (string)$ip . '<br>';
}
$networks = Range::parse('192.168.1.1-192.168.1.254')->getNetworks();
foreach($networks as $network) {
echo (string)$network . '<br>';
}
echo count(Range::parse('192.168.1.1-192.168.1.254')) // 254