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
Newsletter
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
  • Running an OpenStreetMap Tile Server for the Whole Planet on Modest Hardware
  • From Database Changes to Real-Time Actions at Scale: Oracle GoldenGate and Java
  • How (and Why) to Build Dynamic Data Pipelines

Trending

  • Introduction to Error Prone: How to Catch Bugs at Compile Time
  • Save To Bookmarks/Favorites
  • AI-Era Cybersecurity, Part 1: Mapping Glasswing, Daybreak, Lightwell and the Tools Racing to Find and Fix Bugs
  • Member Spotlight: Srinivas Chippagiri

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Nov. 29, 19 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
11.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Running an OpenStreetMap Tile Server for the Whole Planet on Modest Hardware
  • From Database Changes to Real-Time Actions at Scale: Oracle GoldenGate and Java
  • How (and Why) to Build Dynamic Data Pipelines

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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