run-tests Type coverage Psalm error level
Determine the geographical location and currency of website visitors based on their IP addresses.
We have forked torann/geoip as it’s almost not actively maintained anymore.
This fork works with modern PHP versions only (8.0+), maintained and includes additional features:
- Support modern Laravel and PHP versions
- Better types (native and PHPDoc)
- Safer file and network functionality
- Ability to prefix cache keys (
cache_prefixconfig option) - Ability to change service using
GEOIP_SERVICEenv var - Updated currencies
- More predictable exceptions
But don’t worry, we are following SemVer 2.0. The package migration is straightforward.
From the command line run:
composer require interaction-design-foundation/laravel-geoip
Run this on the command line from the root of your project:
php artisan vendor:publish --provider="InteractionDesignFoundation\GeoIP\GeoIPServiceProvider" --tag=configand set the GEOIP_SERVICE env variable.
Quick breakdown of the two main options in the configuration file.
To find out more simple open the config/geoip.php file.
To simplify and keep things clean, all third party composer packages, which are needed for a service, are installed separately.
For further configuration options, checkout the services page.
GeoIP uses Laravel’s default caching to store queried IP locations. This is done to reduce the number of calls made to the selected service, as some of them are rate limited.
Options:
allall locations are cachedsomecache only the requesting usernonecaching is completely disabled
There are few options to use the package:
geoip()helper functionInteractionDesignFoundation\GeoIP\Facades\GeoIPfacade
geoip()->getLocation('27.974.399.65'); // Get the location from the provided IP. geoip()->getClientIP(); // Will return the user IP address.
Example of a Location object:
\InteractionDesignFoundation\GeoIP\Location {[ 'ip' => '1.1.1.1', 'iso_code' => 'US', 'country' => 'United States', 'city' => 'New Haven', 'state' => 'CT', 'state_name' => 'Connecticut', 'postal_code' => '06510', 'lat' => 41.28, 'lon' => -72.88, 'timezone' => 'America/New_York', 'continent' => 'NA', 'currency' => 'USD', 'default' => false, ]}
Location class implements \ArrayAccess interface, means you can access properties of the Location object using both object and array access:
$location = geoip()->getLocation(); $city = $location->city; // The same as: $city = $location['city'];
Some services require downloading and use a local database to detect Location by IP address. There is a console command to download/update database:
php artisan geoip:update
Some cache drivers offer the ability to clear cached locations:
php artisan geoip:clear
Please see Releases for more information on what has changed recently.
Please see CONTRIBUTING for details.
Many people have contributed to the project since its inception.
Thanks to:
Alies Lapatsin Torann
Daniel Stainback dotpack
Ilia Ermolin lptn
alies-lptn dwightwatson
Dwight Watson jessarcher
Jess Archer
Flavius Cosmin kyranb
Kyran osmhub
osmhub mithredate
Mehrdad Pythagus
Damien MOLINA rjp2525
Reno Philibert
Micheal Mand LukeT
Luke Thompson janicerar
Jani Cerar Butochnikov
Alexey ncla
ncla acidjazz
kevin olson
Jamal Al-Matari irtaza100
irtaza100 evaldas-leliuga
Evaldas Leliūga szepeviktor
Viktor Szépe rasoulrahimii
Rasoul Rahimii PhoenixPeca
Phoenix Eve Aspacio