PHP code example of spomky-labs / ip-filter-bundle

1. Go to this page and download the library: Download spomky-labs/ip-filter-bundle 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/ */

    

spomky-labs / ip-filter-bundle example snippets



// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new SpomkyLabs\IpFilterBundle\SpomkyLabsIpFilterBundle(),
    );
}


// src/Acme/IpBundle/Entity/Ip.php

namespace Acme\IpBundle\Entity;

use SpomkyLabs\IpFilterBundle\Entity\Ip as BaseIp;
use Doctrine\ORM\Mapping as ORM;

/**
 * Ip
 *
 * @ORM\Table(name="ips")
 */
class Ip extends BaseIp
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function getId()
    {
        return $this->id;
    }
}


// src/Acme/IpBundle/Entity/Range.php

namespace Acme\IpBundle\Entity;

use SpomkyLabs\IpFilterBundle\Entity\Range as BaseRange;
use Doctrine\ORM\Mapping as ORM;

/**
 * Range
 *
 * @ORM\Table(name="ranges")
 */
class Range extends BaseRange
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function getId()
    {
        return $this->id;
    }
}



$ip_manager    = $this->container->get('sl_ip_filter.ip_manager'); //Use this line, even if you use a custom IP manager
$range_manager = $this->container->get('sl_ip_filter.range_manager'); //Use this line, even if you use a custom Range manager

//Create your IP
$ip = $ip_manager->createIp();
$ip->setIp('192.168.1.10')
   ->setEnvironment('dev,test')
   ->setAuthorized(true);
$ip_manager->saveIp($ip);

//Create your range
$range = $range_manager->createRange();
$range->setStartIp('0.0.0.1')
      ->setEndIp('255.255.254')
      ->setEnvironment('dev,test')
      ->setAuthorized(false);
$range_manager->saveRange($range);



$range_manager = $this->container->get('sl_ip_filter.range_manager');

//All addresses (IPv4)
$range1 = $range_manager->createRangeFromNetwork('0.0.0.0/0');
$range1->setEnvironment('dev,test')
       ->setAuthorized(false);
$range_manager->saveRange($range1);

//My local network (IPv4)
$range2 = $range_manager->createRangeFromNetwork('192.168.0.0/16');
$range2->setEnvironment('dev,test')
       ->setAuthorized(true);
$range_manager->saveRange($range2);

//Another local network (IPv6)
$range3 = $range_manager->createRangeFromNetwork('fe80::/64');
$range3->setEnvironment('dev,test')
       ->setAuthorized(true);
$range_manager->saveRange($range3);