Improved GeoDetect featuring flag recognition

  • avatar
  • 914 Views
  • 1 Like
  • 4 mins read

Collecting and analyzing data on users' geographical locations has become increasingly essential for various applications. Precise geolocation data has the potential to significantly enhance the user experience by personalizing content, customizing marketing strategies, and implementing features specific to regions. Back in May, we introduced the GeoDetect package to facilitate this process.

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.

Prerequisites

Take a look on how to install Composer 2 on your computer to manage packages and its dependencies.

Installation

Assuming that Composer is already installed, you can include the package using Composer:

composer require hibit-dev/geodetect

Usage

Once installed, include the GeoDetect package in your class:

use Hibit\\GeoDetect;

Instantiate the class or use dependency injection:

$geoDetect = new GeoDetect();

Retrieve country record providing user's IP address:

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

Finally, use available helpers to retrieve the required country information:

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

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

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:

<img alt="Country: FR" src="{{Hibit\\GeoDetect::getFlagByIsoCode('FR')}}">

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:

use Hibit\\Flag\\Format;

Hibit\\GeoDetect::getFlagByIsoCode('FR', Format::SVG) // SVG format
Hibit\\GeoDetect::getFlagByIsoCode('FR', Format::H20) // Height: 20px Width: ~30px
Hibit\\GeoDetect::getFlagByIsoCode('FR', Format::H24) // Height: 24px Width: ~36px
Hibit\\GeoDetect::getFlagByIsoCode('FR', Format::W20) // Width: 20px Height: ~13px
Hibit\\GeoDetect::getFlagByIsoCode('FR', 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.

Conclusion

GeoDetect is a useful PHP tool that easily identifies countries based on IP addresses. It integrates smoothly, provides precise results, and performs efficiently, making it a favorite for developers requiring reliable geolocation capabilities. Whether crafting an e-commerce platform, managing content, or exploring analytics, GeoDetect delivers essential country information derived from IP addresses.

Credits

Official GitHub: https://github.com/hibit-dev/geodetect

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

We never send SPAM nor unsolicited emails

0 Comments

Leave a Reply

Your email address will not be published.

Replying to the message: View original

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.