Determine the geographical location and currency of website visitors based on their IP addresses.
Actively maintained fork of
torann/geoipwith modern PHP/Laravel support, better types, and additional features. Migration guide.
composer require interaction-design-foundation/laravel-geoipPublish the config file:
php artisan vendor:publish --provider="InteractionDesignFoundation\GeoIP\GeoIPServiceProvider" --tag=configSet the GEOIP_SERVICE env variable to one of the supported services.
Use the geoip() helper or the GeoIP facade:
// Get location for an IP
$location = geoip('203.0.113.1');
// Get location for the current visitor
$location = geoip()->getLocation();
// Access location data
$location->city; // "New Haven"
$location->country; // "United States"
$location->iso_code; // "US"
$location->timezone; // "America/New_York"
$location->currency; // "USD"The Location object contains: ip, iso_code, country, city, state, state_name, postal_code, lat, lon, timezone, continent, currency, default, and cached.
It implements ArrayAccess, so both $location->city and $location['city'] work.
Set the service via the GEOIP_SERVICE env variable or in config/geoip.php:
| Service | Key | Requires |
|---|---|---|
| MaxMind Database | maxmind_database |
geoip2/geoip2 package + license key |
| MaxMind Web API | maxmind_api |
geoip2/geoip2 package + user ID & license key |
| IP-API | ipapi |
API key (for HTTPS) |
| IPGeolocation | ipgeolocation |
API key |
| IPData | ipdata |
API key |
| IPFinder | ipfinder |
API key |
| IP2Location | ip2location |
API key |
For detailed service configuration, see services documentation.
Key options in config/geoip.php:
GeoIP caches lookups using Laravel's cache system to reduce API calls. Set the cache option to:
all-- cache all lookupssome-- cache only the current user's lookupnone-- disable caching
You can also configure cache_tags, cache_expires (TTL in seconds), and cache_prefix.
When include_currency is enabled (default), the package resolves the visitor's currency from their country ISO code using the league/iso3166 package.
Configure a fallback location returned when a lookup fails or the IP is local/invalid.
# Download/update the local GeoIP database (required for maxmind_database)
php artisan geoip:update
# Clear cached locations
php artisan geoip:clearSee Releases for what has changed recently.
See CONTRIBUTING for details.