Provides information about geographical location of user by IP address.
Currently available:
- Country
- City
- Latitude, Longitude
- Country ISO Code
Run
$ php composer.phar require lysenkobv/yii2-geoip "~1.0"
add to your composer.json
{
"require": {
"lysenkobv/yii2-geoip": "~1.0"
}
}
and run
$ php composer update
<?php
$config = [
...
'components' => [
'geoip' => ['class' => 'lysenkobv\GeoIP\GeoIP'],
]
...
];
somewhere in code
$ip = Yii::$app->geoip->ip(); // current user ip
$ip = Yii::$app->geoip->ip("208.113.83.165");
$ip->city; // "San Francisco"
$ip->country; // "United States"
$ip->location->lng; // 37.7898
$ip->location->lat; // -122.3942
$ip->isoCode; // "US"
$geoip = new \lysenkobv\GeoIP\GeoIP();
$ip = $geoip->ip("208.113.83.165");
$ip->city; // "San Francisco"
$ip->country; // "United States"
$ip->location->lng; // 37.7898
$ip->location->lat; // -122.3942
$ip->isoCode; // "US"
<?php
$config = [
...
'components' => [
'geoip' => [
'class' => 'lysenkobv\GeoIP\GeoIP',
'dbPath' => Yii::getAlias('@example/maxmind/database/city.mmdb')
],
]
...
];
Thanks to davicrystal, krukru
This product includes GeoLite2 data created by MaxMind, available from http://www.maxmind.com