Package Data | |
---|---|
Maintainer Username: | timvdalen |
Maintainer Contact: | tim@code-orange.nl (Tim van Dalen) |
Package Create Date: | 2017-11-21 |
Package Last Update: | 2017-11-28 |
Home Page: | https://code-orange.github.io/lumen-geoip/ |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-15 15:11:57 |
Package Statistics | |
---|---|
Total Downloads: | 7,667 |
Monthly Downloads: | 21 |
Daily Downloads: | 0 |
Total Stars: | 1 |
Total Watchers: | 5 |
Total Forks: | 2 |
Total Open Issues: | 0 |
Determine the geographical location of website visitors based on their IP addresses.
To install this package, just install through composer
$ composer require code-orange/lumen-geoip
Next, open bootstrap/app.php
and add under the Register Service Providers section:
...
$app->register(CodeOrange\GeoIP\GeoIPServiceProvider::class);
Run this on the command line from the root of your project:
$ php artisan geoip:update
GeoIP will try to determine the IP using the following http headers: HTTP_CLIENT_IP
, HTTP_X_FORWARDED_FOR
, HTTP_X_FORWARDED
, HTTP_X_CLUSTER_CLIENT_IP
, HTTP_FORWARDED_FOR
, HTTP_FORWARDED
, REMOTE_ADDR
in this order. Optionally you can set an IP as the only parameter to set it.
$record = app()->geoip->getLocation('232.223.11.11');
$record = GeoIP::getLocation('232.223.11.11'); // If you have enabled facades
print($record->country->isoCode . "\n"); // 'US'
print($record->country->name . "\n"); // 'United States'
print($record->country->names['zh-CN'] . "\n"); // '美国'
print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota'
print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN'
print($record->city->name . "\n"); // 'Minneapolis'
print($record->postal->code . "\n"); // '55455'
print($record->location->latitude . "\n"); // 44.9733
print($record->location->longitude . "\n"); // -93.2323
These methods are also available to use within your applications.
app()->geoip->checkIp($ip) // Checks IP to make sure IP is a valid IPv4 or IPv6 address and not within a private or reserved range
app()->geoip->getIp() // Returns the detected client IP
When an IP is not detected it will be set to 127.0.0.1 which will ultimately throw an Exception. If you are not in production your record will default to the following data.
array (
"ip" => "232.223.11.11",
"isoCode" => "US",
"country" => "United States",
"city" => "New Haven",
"state" => "CT",
"postal_code" => "06510",
"lat" => 41.28,
"lon" => -72.88,
"timezone" => "America/New_York",
"continent" => "NA",
"default" => false
);