Geolocation Lookup Using Symfony and IP2Location BIN Database

Geolocation Lookup Using Symfony 4 and IP2Location BIN Database

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

Step 1: Run the following command to download the package into the Symfony 4 platform.
> composer require ip2location/ip2location-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 project using the below command line.
> php bin/console make:controller DefaultController

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

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

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

<h3>IP2Location Symfony 4 Demo</h3>

<p>This demo uses <a href="https://lite.ip2location.com/database/ip-country-region-city-latitude-longitude-zipcode-timezone" target="_blank">IP2Location DB11 LITE BIN Data</a> for geolocation 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 Number: </td>
                <td>{{ records.ipNumber }}</td>
            </tr>
            <tr>
                <td>IP Version: </td>
                <td>{{ records.ipVersion }}</td>
            </tr>
            <tr>
                <td>IP Address: </td>
                <td>{{ records.ipAddress }}</td>
            </tr>
            <tr>
                <td>Country Code: </td>
                <td>{{ records.countryCode }}</td>
            </tr>
            <tr>
                <td>Country Name: </td>
                <td>{{ records.countryName }}</td>
            </tr>
            <tr>
                <td>Region Name: </td>
                <td>{{ records.regionName }}</td>
            </tr>
            <tr>
                <td>City Name: </td>
                <td>{{ records.cityName }}</td>
            </tr>
            <tr>
                <td>Time Zone: </td>
                <td>{{ records.timeZone }}</td>
            </tr>
            <tr>
                <td>ZIP Code: </td>
                <td>{{ records.zipCode }}</td>
            </tr>
            <tr>
                <td>Latitude: </td>
                <td>{{ records.latitude }}</td>
            </tr>
            <tr>
                <td>Longitude: </td>
                <td>{{ records.longitude }}</td>
            </tr>
            <tr>
                <td>Area Code: </td>
                <td>{{ records.areaCode }}</td>
            </tr>
            <tr>
                <td>IDD Code: </td>
                <td>{{ records.iddCode }}</td>
            </tr>
            <tr>
                <td>Weather Station Code: </td>
                <td>{{ records.weatherStationCode }}</td>
            </tr>
            <tr>
                <td>Weather Station Name: </td>
                <td>{{ records.weatherStationName }}</td>
            </tr>
            <tr>
                <td>MCC: </td>
                <td>{{ records.mcc }}</td>
            </tr>
            <tr>
                <td>MNC: </td>
                <td>{{ records.mnc }}</td>
            </tr>
            <tr>
                <td>Mobile Carrier: </td>
                <td>{{ records.mobileCarrierName }}</td>
            </tr>
            <tr>
                <td>Usage Type: </td>
                <td>{{ records.usageType }}</td>
            </tr>
            <tr>
                <td>Elevation: </td>
                <td>{{ records.elevation }}</td>
            </tr>
            <tr>
                <td>Net Speed: </td>
                <td>{{ records.netSpeed }}</td>
            </tr>
            <tr>
                <td>Domain Name: </td>
                <td>{{ records.domainName }}</td>
            </tr>
            <tr>
                <td>ISP Name: </td>
                <td>{{ records.isp }}</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();
            //Your BIN database file path
            $db = new \IP2Location\Database ('./src/Database/IP2LOCATION.BIN', \IP2Location\Database::FILE_IO);

            $records = $db->lookup($ip, \IP2Location\Database::ALL);

            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.

IP2Location LITE database

Was this article helpful?

Related Articles