interaction-design-foundation / laravel-geoip
Support for multiple GeoIP services.
Installs: 25 156
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 0
Forks: 318
Open Issues: 0
Requires
- php: ^8.0
- illuminate/cache: ^8.0|^9.0
- illuminate/console: ^8.0|^9.0
- illuminate/support: ^8.0|^9.0
Requires (Dev)
- geoip2/geoip2: ~2.1
- mockery/mockery: ^1.4
- phpstan/phpstan: ^1.2
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
- vlucas/phpdotenv: ^5.0
Suggests
- geoip2/geoip2: Required to use the MaxMind database or web service with GeoIP (~2.1).
- monolog/monolog: Allows for storing location not found errors to the log
README
Determine the geographical location and currency of website visitors based on their IP addresses.
About this fork
We have forked Torann/laravel-geoip as it’s almost not maintained anymore. This fork works with modern PHP versions only (8.0+). Also, as for any InteractionDesignFoundation project, we are going to improving code quality by using types, static analysers, tests and linters. But don’t worry, we are using SemVer 2.0.
Installation
From the command line run:
composer require interaction-design-foundation/laravel-geoip
Publish the configurations
Run this on the command line from the root of your project:
php artisan vendor:publish --provider="InteractionDesignFoundation\GeoIP\GeoIPServiceProvider" --tag=config
Configuration
Quick breakdown of the two main options in the configuration file.
To find out more simple open the config/geoip.php
file.
Service Configuration
To simplify and keep things clean, all third party composer packages, that are needed for a service, are installed separately.
For further configuration options checkout the services page.
Caching Configuration
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:
all
all location are cachedsome
cache only the requesting usernone
caching is completely disable
Usage
There are few options to use the package:
geoip()
helper functionInteractionDesignFoundation\GeoIP\Facades\GeoIP
facade
geoip()->getLocation('27.974.399.65'); // Get the location from the provided IP. geoip()->getClientIP(); // Will return the user IP address.
Example of 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 array access syntax:
$location = geoip()->getLocation(); $city = $location['city'];
Artisan
Some services require downloading and use 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
Changelog
Please see Releases for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Contributions
Many people have contributed to project since its inception.
Thanks to:
- Daniel Stainback (creator of the original package)
- Dwight Watson
- nikkiii
- jeffhennis
- max-kovpak
- dotpack
- Jess Archer