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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Symfony vs Laravel: Why Symfony Is Better Than Laravel
  • Useful System Table Queries in Relational Databases
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide

Trending

  • Integrating Model Context Protocol (MCP) With Microsoft Copilot Studio AI Agents
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • Building Reliable LLM-Powered Microservices With Kubernetes on AWS
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 2
  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.1K 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
  • Useful System Table Queries in Relational Databases
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!