PHPackages                             hibit-dev/geodetect - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. hibit-dev/geodetect

ActiveLibrary

hibit-dev/geodetect
===================

Automatically detect user's geo data based on their IP address

v3.2.1(2mo ago)2319.5k↓26.8%4MITPHPPHP &gt;=8.2CI passing

Since May 15Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/hibit-dev/geodetect)[ Packagist](https://packagist.org/packages/hibit-dev/geodetect)[ Docs](https://www.hibit.dev)[ RSS](/packages/hibit-dev-geodetect/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (35)Used By (0)

[![Automatically detect user's geo data based on their IP address](https://raw.githubusercontent.com/hibit-dev/geodetect/master/images/preview.png)](https://raw.githubusercontent.com/hibit-dev/geodetect/master/images/preview.png)

[![GitHub Actions Workflow Status](https://camo.githubusercontent.com/975cc7b924600126d764012634237fd1628b37f16e3ab226ea904a92cfa665b0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f68696269742d6465762f67656f6465746563742f2e676974687562253246776f726b666c6f77732532466275696c642e796d6c)](https://github.com/hibit-dev/geodetect/actions)[![GitHub License](https://camo.githubusercontent.com/ed4039bfc136ef1eb20e579dbbd70c08f6a2dc8388da44f609a55b79ca4285ad/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f68696269742d6465762f67656f646574656374)](https://github.com/hibit-dev/geodetect)[![Packagist Version](https://camo.githubusercontent.com/c193691ec99af53ac6f6a65ddc546c35de7f358dc8a424fe34ccf73d723edc19/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68696269742d6465762f67656f646574656374)](https://packagist.org/packages/hibit-dev/geodetect)[![Packagist Downloads](https://camo.githubusercontent.com/597d38d3f29621741983520c7c1f263f78b612fe0d3d431a02519cc299d27a71/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68696269742d6465762f67656f646574656374)](https://packagist.org/packages/hibit-dev/geodetect)[![Tests Coverage](https://camo.githubusercontent.com/11b56f428a260ba58ac7324f8f552b57f6df79b8e2e6131daf9ca2742929faec/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d68696269742d6465765f67656f646574656374266d65747269633d636f766572616765)](https://sonarcloud.io/summary/new_code?id=hibit-dev_geodetect)

GeoDetect: IP-based country detection
=====================================

[](#geodetect-ip-based-country-detection)

GeoDetect is a framework-agnostic PHP package that allows to effortlessly extract valuable country information from IP addresses. Powered by a robust and up-to-date IP geolocation database, the package provides accurate results and ensures reliable performance. By Integrating GeoDetect into your PHP applications developers can easily implement geolocation functionality without the hassle of building and maintaining their own IP geolocation database.

Installation
------------

[](#installation)

Install GeoDetect using `composer require`:

```
composer require hibit-dev/geodetect
```

Usage
-----

[](#usage)

Once installed, include the GeoDetect package in your class:

```
use Hibit\GeoDetect;
```

Instantiate the class or use the dependency injection:

```
$geoDetect = new GeoDetect();
```

### Countries

[](#countries)

Retrieve country record providing user's IP address:

```
$country = $geoDetect->getCountry('XXX.XXX.XXX.XXX');
```

Use available helpers to retrieve the required country information:

```
$country->getGeonameId();
$country->getIsoCode();
$country->getName();
$country->isInEuropeanUnion();
```

Also, continent related information:

```
$country->continent()->getGeonameId();
$country->continent()->getIsoCode();
```

### Self-hosted database

[](#self-hosted-database)

The package utilizes MaxMind's database in the background, which is regularly updated to ensure the accuracy of the data. However, you also have the option to use a self-hosted database by specifying the location of the DB file:

```
$country = $geoDetect->setCountriesDatabase('location_to_db_file')
                     ->getCountry('XXX.XXX.XXX.XXX');
```

> The package includes [MaxMind](https://www.maxmind.com)'s database of 2026-03-13

### Printing user flag

[](#printing-user-flag)

The GeoDetect package offers the capability to easily display a user's flag. Simply create an image HTML tag and insert the corresponding source for the image based on the country's ISO2 code:

```

```

By default, the resulting flag will have a height of 20px and a width that typically varies around 30px.

Sizes can be adjusted and even replaced with an SVG image if needed. To alter the format, the getFlagByIsoCode method requires a second parameter to be provided:

```
Hibit\GeoDetect::getFlagByIsoCode('FR', Hibit\Flag\Format::SVG) // SVG format
Hibit\GeoDetect::getFlagByIsoCode('FR', Hibit\Flag\Format::H20) // Height: 20px Width: ~30px
Hibit\GeoDetect::getFlagByIsoCode('FR', Hibit\Flag\Format::H24) // Height: 24px Width: ~36px
Hibit\GeoDetect::getFlagByIsoCode('FR', Hibit\Flag\Format::W20) // Width: 20px Height: ~13px
Hibit\GeoDetect::getFlagByIsoCode('FR', Hibit\Flag\Format::W40) // Width: 40px Height: ~26px
```

*Note: FR was used for illustrative purposes; obtain the country code through the getIsoCode method of the country record. Alternatively, use the 2-character ISO code of the country if it's already available from another source.*

Implementation in Laravel
-------------------------

[](#implementation-in-laravel)

To facilitate testing, we will create a new route within our Laravel application. In the *routes/web.php* file, a new route called *get-country* has been added. When this route is accessed, it will return all the available information in JSON format.

```
Route::get('/get-country', function (Illuminate\Http\Request $request) {
    $geoDetect = new Hibit\GeoDetect();

    $country = $geoDetect->getCountry($request->getClientIp());

    return response()->json([
        'continent' => [
            'geonameId' => $country->continent()->getGeonameId(),
            'isoCode' => $country->continent()->getIsoCode(),
        ],
        'geonameId' => $country->getGeonameId(),
        'isoCode' => $country->getIsoCode(),
        'name' => $country->getName(),
        'isInEuropeanUnion' => $country->isInEuropeanUnion(),
    ]);
});
```

Please note that when testing the country detection functionality locally, it may not work as expected. This is because the client IP address in the request instance is typically set to localhost (127.0.0.1). Keep this in mind while testing the country detection feature on your local development environment.

### Country names translation

[](#country-names-translation)

The package comes with configured discovery option to enable Laravel to automatically identify and publish the necessary translations. You can find the translations in the */lang/en/geodetect.php* file, and feel free to make any modifications to the translations as needed. If the files are not present, please manually publish them by executing the following command:

```
php artisan vendor:publish --tag=hibit-geodetect
```

Once the translations have been published, you can use them to display the country name in any blade file using the translation directive:

```
@lang('geodetect.ES') //Output: Spain
```

Alternatively, the following syntax can be used outside the blades:

```
__('geodetect.ES') //Output: Spain
```

Documentation
-------------

[](#documentation)

Discover a world of knowledge hosted on [HiBit website](https://www.hibit.dev). Serving as your informational hub, this resource offers clear instructions and valuable insights to explore a spectrum of articles, tutorials, stories, news, and beyond.

You'll find detailed instructions and comprehensive documentation about this repository on:

- [GeoDetect: PHP package for IP-based country detection](https://www.hibit.dev/posts/105/geodetect-php-package-for-ip-based-country-detection)
- [Country detection in Laravel applications](https://www.hibit.dev/posts/115/country-detection-in-laravel-applications)
- [Improved GeoDetect featuring flag recognition](https://www.hibit.dev/posts/133/improved-geodetect-featuring-flag-recognition)

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

About HiBit
-----------

[](#about-hibit)

[HiBit](https://www.hibit.dev) isn't just a blog; it's your go-to space for everything related to development, IT, and the wonders of electronics. Designed for developers, IT enthusiasts, and electronics hobby lovers, HiBit is a dynamic hub that keeps you in the loop with fresh and engaging content.

Explore a collection of articles, tutorials, and insights, encouraging a lively community where reading, commenting, discussing, and sharing experiences is not just promoted but celebrated.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance87

Actively maintained with recent releases

Popularity37

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 98.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~31 days

Recently: every ~40 days

Total

34

Last Release

64d ago

Major Versions

v1.3.0 → v2.0.02023-11-25

v2.17.0 → v3.0.02025-03-15

PHP version history (3 changes)v1.0.0PHP &gt;=8.0

v2.0.0PHP &gt;=8.1

v3.0.0PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/79d98b35bfad87e67663d7d970fb1a71809367e7bd1a8fd770a826d690f5758b?d=identicon)[tmekinyan](/maintainers/tmekinyan)

---

Top Contributors

[![tmekinyan](https://avatars.githubusercontent.com/u/15942542?v=4)](https://github.com/tmekinyan "tmekinyan (76 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (1 commits)")

---

Tags

symfonylaravelIPlocationgeocountrydetection

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/hibit-dev-geodetect/health.svg)

```
[![Health](https://phpackages.com/badges/hibit-dev-geodetect/health.svg)](https://phpackages.com/packages/hibit-dev-geodetect)
```

###  Alternatives

[stevebauman/location

Retrieve a user's location by their IP Address

1.3k7.6M65](/packages/stevebauman-location)[kartik-v/yii2-ipinfo

An IP address information display widget for Yii 2.0 with country flag and geo position info.

2832.6k1](/packages/kartik-v-yii2-ipinfo)[nevadskiy/laravel-geonames

Populate your database using the GeoNames service.

2715.1k](/packages/nevadskiy-laravel-geonames)[dive-be/laravel-geo

Translate IP addresses into geo locations

3710.5k](/packages/dive-be-laravel-geo)[interaction-design-foundation/laravel-geoip

Support for multiple Geographical Location services.

17221.0k3](/packages/interaction-design-foundation-laravel-geoip)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
