How to get country from ip in laravel?

Member

by dana , in category: PHP Frameworks , 9 months ago

How to get country from ip in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jasen , 9 months ago

@dana 

To get the country from an IP address in Laravel, you can use a package like "torann/geoip". Here's how you can do it:

  1. Install the package using composer:
1
composer require torann/geoip


  1. Publish the package configuration file:
1
php artisan vendor:publish --provider="TorannGeoIPGeoIPServiceProvider" --tag=config


  1. Update the configuration file "config/geoip.php" with the correct database path:
1
'database_path' => storage_path('app/geoip.mmdb'),


  1. To get the country from an IP address, you can use the GeoIP facade like this:
1
2
3
4
use TorannGeoIPFacadesGeoIP;

$ip = '192.168.1.1';
$country = GeoIP::getLocation($ip)->country;


That's it! You can now use the "country" variable to get the country from the IP address.

Related Threads:

How to get client ip address in laravel?
How to get a list of country codes in woocommerce?
How to get client real ip address in php?
How to restrict specific ip addresses to xampp?
How to redirect based on country?
How to add country and state in woocommerce?