How to create a simple IP address tracker with geolocation in Symfony 6+

Do you want to know who visits your website, where they’re from, and when?
In this post, I’ll show you how to build a small Symfony project that:

  • Detects the visitor’s IP address
  • Retrieves geolocation data based on the IP
  • Stores this data in a database
  • Displays the IP, country, and city on the homepage

⚙️ Project Setup

Creating a new Symfony project:

composer create-project symfony/skeleton:"6.4.*" ip-tracker  
cd ip-tracker

Installing required dependencies:

composer require symfony/twig-bundle  
composer require symfony/http-client  
composer require symfony/orm-pack doctrine/doctrine-bundle  
composer require nesbot/carbon  
composer require symfony/maker-bundle --dev

🧱 Creating the Controller

File:

src/Controller/ClientIpController.php

<?php

namespace App\Controller;

use App\Entity\VisitorLog;
use App\Service\IpGeolocationService;
use Carbon\Carbon;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class ClientIpController extends AbstractController
{
    #[Route('/', name: 'app_home')]
    public function index(Request $request, IpGeolocationService $ipService, EntityManagerInterface $em): Response
    {
        $ip = $request->getClientIp();
        $geo = $ipService->getGeolocation($ip);

        $log = new VisitorLog();
        $log->setIp($ip);
        $log->setCountry($geo['country'] ?? null);
        $log->setCity($geo['city'] ?? null);
        $log->setCreatedAt(Carbon::now());

        $em->persist($log);
        $em->flush();

        return $this->render('client_ip/index.html.twig', [
            'ip' => $ip,
            'geo' => $geo,
        ]);
    }
}

🌐 Geolocation Service

File:

src/Service/IpGeolocationService.php

<?php

namespace App\Service;

use Symfony\Contracts\HttpClient\HttpClientInterface;

class IpGeolocationService
{
    public function __construct(private HttpClientInterface $client) {}

    public function getGeolocation(string $ip): array
    {
        $response = $this->client->request('GET', "https://ipapi.co/{$ip}/json/");
        return $response->toArray();
    }
}

🗃 Creating an Entity for Logging

Command:

php bin/console make:entity VisitorLog

Fields:

  • ip

    : string

  • country

    : string (nullable)

  • city

    : string (nullable)

  • createdAt

    : datetime_immutable

Migrations:

php bin/console make:migration  
php bin/console doctrine:migrations:migrate

🖥 Template for Displaying Information to Users

File:

templates/client_ip/index.html.twig

{% extends ‘base.html.twig’ %}

{% block title %}Your IP Address is: {{ ip_address }}{% endblock %}

{% block body %}
<div class="ip-address-wrapper"><h1>Your IP Address is: {{ ip_address }}</h1></div>
{% if geo %}
<ul class="ip-info">
<li>Country: {{ geo.country }}</li>
<li>City: {{ geo.city }}</li>
</ul>
{% endif %}
{% endblock %}


📦 Project Dependencies

According to the list in

composer.json

, this project uses:

  • Symfony 6.4
  • Doctrine ORM
  • Twig
  • Carbon
  • HttpClient
  • MakerBundle (dev)

📝 Conclusion

Together we’ve built a full-featured IP logger on Symfony:

✅ Detects IP
✅ Retrieves geolocation
✅ Stores data in the database
✅ Displays it on the page

This functionality can serve as the foundation for a more complete analytics system, access control, or content personalization for your users.

Leave a Reply

Your email address will not be published. Required fields are marked *