smalldogs / ip2country
Laravel package to lookup the country associated with an IPv4 address
Installs: 1 005
Dependents: 0
Suggesters: 0
Security: 0
Stars: 22
Watchers: 5
Forks: 7
Open Issues: 2
Requires
- php: >=5.4.0
- illuminate/support: 4.2.*
This package is not auto-updated.
Last update: 2017-08-21 08:50:13 UTC
README
Laravel package to lookup the country associated with an IPv4 address. Developed with an eye to keeping it as lightweight and lookups as fast as possible. Creates and populates a local database table, so there are no external requests being made during runtime.
This package includes GeoLite data created by MaxMind, available from http://www.maxmind.com. The updated free downloadable database is released the first Tuesday of each month. I'll attempt to ensure to update this package each time.
Current IP Mapping Table
Doesn't seem to change much from month to month. Current version was released by MaxMind February 4, 2015
How to Update Mapping DB Table
If you have already installed and setup the ip2country package, but want to update the IP database mappings. First, get the latest ip2country package.
composer update
or, to only update this pacakage
composer update smalldogs/ip2country
Then, update your database by running the new migration.
php artisan migrate --package="smalldogs/ip2country"
How to Install
1. Require the package with composer.
composer require "smalldogs/ip2country"
2. Create and populate the database lookup table.
php artisan migrate --package="smalldogs/ip2country"
3. Add the service to your providers array in app/config/app.php
'providers' => array( 'Smalldogs\Ip2Country\Ip2CountryServiceProvider', //[...] );
How to Use
// Returns the 2 letter country code for the user, eg: 'US' $myCountryCode = Ip2Country::get(); // Returns the full name of the country, eg: 'United States' $myCountryName = Ip2Country::getFull(); // Get the country for someone other than user on the page $someonesIpAddress = '192.168.0.1'; $someonesCountryCode = Ip2Country::get($someonesIpAddress);
Configuration
By default, if the IP address is not found in the lookup table, it will return 'US' as a country code, and 'United States' as a country name. You can customize this in the config.
php artisan config:publish smalldogs/ip2country
Then navigate to app/config/packages/smalldogs/ip2country/config.php
.
return array( // If IP address is not found, what country is returned 'default_country_code' => 'US', 'default_country_name' => 'United States', // Since our data will change, at most, once a month. Cache the Ip lookup for a day // Default: 1 day. Set to 0 or null to disable. 'cache_results' => 60 * 24 );
If you don't want anything returned if the IP address is not found, simply set each of these to => null