Proxy Lookup Using Symfony 4 and IP2Proxy BIN Database

Proxy Lookup Using Symfony 4 and IP2Proxy BIN Database

This demo supports Symfony 4, 5, and 6. In this tutorial, we’ll show you how to perform proxy lookup and display the IP proxy information from a visitor’s IP using Symfony platform and IP2Proxy BIN database. This tutorial uses the IP2Proxy module, available at https://www.ip2location.com/development-libraries/ip2proxy/php, to query IP information from BIN database. Free BIN databases are available for download at IP2Proxy LITE database.

Step 1: Run the following command to download the package into the Symfony 4 platform.
> composer require ip2location/ip2proxy-php

Step 2: In this tutorial, we’ll use and include the database in our own project in the /src/Database directory of our Symfony project (note that this directory doesn’t exist and therefore needs to be created, you can change the path of the database according to your needs).

Step 3: Create a DefaultController in Symfony using the below command line.
> php bin/console make:controller DefaultController

Step 4: Add the below lines to index.html.twig file.

{% extends 'base.html.twig' %}

{% block body %}
<style>
h3, p, form {
    padding-left: 25px;
}
</style>

<h3>IP2Proxy Symfony 4 Demo</h3>

<p>This demo uses <a href="https://lite.ip2location.com/database/px10-ip-proxytype-country-region-city-isp-domain-usagetype-asn-lastseen-threat-residential" target="_blank">IP2Proxy PX10 LITE BIN Data</a> for proxy lookup. You may download the BIN Data at <a href="https://lite.ip2location.com" target="_blank">https://lite.ip2location.com</a> (Free edition) or <a href="https://www.ip2location.com" target="_blank">https://www.ip2location.com</a> (Commercial edition)</p>

{% if form is defined %}
<form id="form" action="" method="POST">
    <table>
         <tr>
             <td>IP Address: </td>
             <td style="padding-left:10px">{{form_widget(form.ip)}}</td>
             <td style="padding-left:10px"><input class="btn btn-primary" type="submit" value="Submit" name="submit"/></td>
        </tr>
    </table>
</form>
{% endif %}
<br/>

{% if records is defined  %}
<div>
    <table class="table table-bordered table-striped">
        <tbody>
            <tr>
                <td>IP Address: </td>
                <td>{{ records.ipAddress }}</td>
            </tr>
            <tr>
                <td>IP Number: </td>
                <td>{{ records.ipNumber }}</td>
            </tr>
            <tr>
                <td>IP Version: </td>
                <td>{{ records.ipVersion }}</td>
            </tr>
            <tr>
                <td>Country Code: </td>
                <td>{{ records.countryCode }}</td>
            </tr>
            <tr>
                <td>Country: </td>
                <td>{{ records.countryName }}</td>
            </tr>
            <tr>
                <td>State: </td>
                <td>{{ records.regionName }}</td>
            </tr>
            <tr>
                <td>City: </td>
                <td>{{ records.cityName }}</td>
            </tr>
            <tr>
                <td>Proxy Type: </td>
                <td>{{ records.proxyType }}</td>
            </tr>
            <tr>
                <td>Is Proxy: </td>
                <td>{{ records.isProxy }}</td>
            </tr>
            <tr>
                <td>Usage Type: </td>
                <td>{{ records.usageType }}</td>
            </tr>
            <tr>
                <td>ISP: </td>
                <td>{{ records.isp }}</td>
            </tr>
            <tr>
                <td>Domain: </td>
                <td>{{ records.domain }}</td>
            </tr>
            <tr>
                <td>ASN: </td>
                <td>{{ records.asn }}</td>
            </tr>
            <tr>
                <td>Last Seen: </td>
                <td>{{ records.lastSeen }}</td>
            </tr>
            <tr>
                <td>Threat: </td>
                <td>{{ records.threat }}</td>
            </tr>
        </tbody>
    </table>
</div>
{% endif %}
{% endblock %}

Step 5: Add the below lines into the DefaultController.php file.

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends AbstractController
{
    /**
     * @Route("/home", name="home")
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function index(Request $request)
    {
        $form = $this->createFormBuilder()
        ->add('ip', TextType::class, [
            'attr' => [
                'class' => 'form-control'
            ]
        ])
        ->add('submit', SubmitType::class, [
            'attr' => [
                'class' => 'btn btn-primary'
            ]
        ])
        ->getForm();

        $form->handleRequest($request);

        if ($form->isSubmitted()) {
            $ip = $form['ip']->getData();

            $db = new \IP2Proxy\Database();
      
            //Your BIN database file path
            $db->open('./src/Database/IP2PROXY.BIN', \IP2Proxy\Database::FILE_IO);

            $records = $db->getAll($ip);

            return $this->render('default/index.html.twig',[
                'form' => $form->createView(),
                'records' => $records
            ]);
        }

        return $this->render('default/index.html.twig', [
            'form' => $form->createView()
        ]);
    }
}

Step 6: You may insert the Bootstrap link into your /templates/base.html.twig file.

Step 7: Enter the URL <your domain>/home and enter the IP address. You should see the information of the entered IP address.

Was this article helpful?

Related Articles