DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Symfony vs Laravel: Why Symfony Is Better Than Laravel
  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)

Trending

  • Mastering Fluent Bit: Beginners' Guide for Contributing to Our CNCF Project Website
  • Migrate a Hardcoded LangGraph Agent to LaunchDarkly AI Configs in 20 Minutes
  • Stop Debugging Glue Jobs Manually: Building an Agentic Observability Layer for Data Pipelines
  • When One MVP Is Really Four Systems: A Better Way to Plan Multi-Role Apps
  1. DZone
  2. Data Engineering
  3. Databases
  4. Geolocation Lookup Using Symfony 4 and IP2Location BIN Database

Geolocation Lookup Using Symfony 4 and IP2Location BIN Database

In this tutorial, see how to display the IP information from a visitor’s IP using Symfony 4 platform and IP2Location BIN database.

By 
tan susan user avatar
tan susan
·
Nov. 29, 19 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
11.3K Views

Join the DZone community and get the full member experience.

Join For Free

Geolocation on cell phone

Geolocation Lookup Using Symfony 4 and IP2Location BIN Database

This demo is only supported for Symfony 4.

In this tutorial, we’ll show you how to display the IP information from a visitor’s IP using Symfony 4 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.

You might also like:  Applying Hexagonal Architecture to a Symfony Project

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();

        $query = $request->request->get('form')['ip'];

        if($query)
        {
      //Your BIN database file path
            $db = new \IP2Location\Database ('./src/Database/IP2LOCATION.BIN', \IP2Location\Database::FILE_IO);

            $records = $db->lookup($query, \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.

Database Symfony

Opinions expressed by DZone contributors are their own.

Related

  • Symfony vs Laravel: Why Symfony Is Better Than Laravel
  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook