PHP code example of littledevnl / iptools

1. Go to this page and download the library: Download littledevnl/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/ */

    

littledevnl / iptools example snippets


use Littledev\IPTools\Address;
use Littledev\IPTools\Network;
use Littledev\IPTools\Iterator\AddressIterator;
use Littledev\IPTools\Family\IPFamily;

$ip = Address::parse($_SERVER['REMOTE_ADDR']);
if ($ip->family() === IPFamily::v6()) {
    echo 'Good for you!';
}

$network6 = Network::parse('2001:db8::/64');
if ($network6->contains($ip)) {
    echo 'You use an IP which is reserved for documentation.';
}

$localNetwork = Network::parse('127.0.0.1/24');
if ($localNetwork->contains($ip)) {
    echo 'No place like localhost.';
}

$iterator = new AddressIterator($localNetwork);
foreach ($iterator as $address) {
    echo 'In local network: ' . $address->address();
}

use Littledev\IPTools\Network;
use Littledev\IPTools\Family\IPFamily;

$net = Network::parse('2001:db8::/64');
$numbersOfAddresses =  bcpow('2', (string)(IPFamily::v6()->maxPrefix() - $net->subnet()->prefix()));