Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram
Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram Pinterest LinkedIn Reddit
Readytocode
You are at:Home Laravel 9 – How to Get User Country, City, ZIP from IP

Laravel

Laravel 9 – How to Get User Country, City, ZIP from IP

JustinBy JustinMay 31, 2022Updated:June 4, 2022No Comments3 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email




This post i will show you , get user location details using his IP. Here i will use Maxmind. Which is best among all others. Every other package is also using Maxmind.

Step 1. Sign Up to Maxmind.

Step 2. Download Database from Maxmind.

Step 3. Install Package through Composer.

Step 4. Setup the Maxmind in Laravel Project.

Step 5. Create Controller and get all users location.

 

Step 1. Sign Up to Maxmind:

Visit this link and register with your email.

https://www.maxmind.com/en/accounts/522958/geoip/downloads

 

Step 2. Download Database from Maxmind:

Maxmind Database download

 

Scroll down and download as like below image.

Maxmind download

 

Step 3. Install Package through Composer:

Before going to install package , please install laravel using the below command.

composer create-project --prefer-dist laravel/laravel laravel9GeoIp

Note: laravel9GeoIp is my project name. You can give our own.

Now install package .

composer require geoip2/geoip2

 

Step 4. Setup the Maxmind in Laravel Project:

A. Create a new directory inside the app/ , With name “Traits”. For reference check below image.

CreateTraits

 

B. Create a new file inside the app/Traits/ , With name “MaxMindGeoIP2Trait.php“.

New PHP file name

 

C. MaxMindGeoIP2Trait.php Open the file and put the below code.

<?php

namespace App\Traits;

use Exception;
use GeoIp2\Database\Reader;
use Illuminate\Support\Str;
use MaxMind\Db\InvalidDatabaseException;
use GeoIp2\Exception\AddressNotFoundException;

trait MaxMindGeoIP2Trait {

 protected function getGeoIp2($IP) {
 $geoDataArray = array();

 try {
 if(filter_var($IP, FILTER_VALIDATE_IP)) {
 $reader = new Reader(base_path().'/database/MaxMindGeoIP2/GeoIP2-City.mmdb');
 $record = $reader->city($IP);

 $geoDataArray = array(
 'cityName' => $record->city->name,
 'latitude' => $record->location->latitude,
 'longitude' => $record->location->longitude,
 'timeZone' => $record->location->timeZone,
 'zipCode' => $record->postal->code,
 'regionName' => $record->mostSpecificSubdivision->name,
 'countryName' => $record->country->name,
 'countryCode' => strtoupper(Str::of($record->country->isoCode)->replace(array(' ', '\\', '/'), array('', '', ''))->trim()->__toString()),
 'ipAddress' => $record->traits->ipAddress,
 );
 }
 } catch (AddressNotFoundException $e) {
 } catch (InvalidDatabaseException $e) {
 } catch (Exception $e) {
 }

 return $geoDataArray;
 }
}

?>
D. Create a new directory inside database/ with name “MaxMindGeoIP2”, For references check below image.
Database Directory
E: Now pest the database which you downloaded from maxmind. Extract and put you can find with name “GeoIP2-City.mmdb“;
Database add Maxmind

Step 5. Create Controller and get all users location.

Create a controller, I am creating with the name of “Location“.

php artisan make:controller Location

 

Open the file and put the below code .

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Traits\MaxMindGeoIP2Trait;


class Location extends Controller
{
    use MaxMindGeoIP2Trait;
    public function index(){
        
        $IPData = $this->getGeoIp2("180.151.89.16");
        dd($IPData);
        return("This is index");
    }
}

 

Also define your route. Open the routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Location;



Route::get('/', function () {
    return view('welcome');
});
Route::get("location",[Location::class,"index"]);

Run this project and check, You will find like below, If you have any problem please comment below.

Result

 

Happy Coding….

Total
1
Shares
Share 0
Tweet 0
Pin it 1
Share 0

Like this:

Like Loading...

Related

City How to Get Location Information How to Get User Country Laravel9 Country from IP ZIP
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Justin
  • Website

Related Posts

How to Generate image from text with Laravel 10

May 16, 2023

Laravel 10 Restrict User Access

May 3, 2023

Laravel 10 Pagination Example

May 3, 2023

Leave A Reply Cancel Reply

Featured Posts
  • Why meta tag is important for seo 1
    How to change react Page title, Page Description and some other meta Tag
    • August 4, 2023
  • 2
    How to Generate image from text with Laravel 10
    • May 16, 2023
  • Laravel 10 Restrict User Access 3
    Laravel 10 Restrict User Access
    • May 3, 2023
  • Laravel 10 Pagination Example 4
    Laravel 10 Pagination Example
    • May 3, 2023
  • install Steam on Ubuntu 5
    How to install Steam on Ubuntu 22.04 step by step
    • April 20, 2023



Readytocode
Facebook X (Twitter) Instagram Pinterest
  • About Us
  • Privacy Policy
© 2025 ReadyToCode.

Type above and press Enter to search. Press Esc to cancel.

%d