IP2Location Yii Extension - Get geolocation data from IP address
This extension use IP2Location BIN database to lookup visitor geographical location by their IP address. You can easily translate the IP address into country code, country name, region name, city name, latitude & longitude, time zone, usage type and so on.
The extension works well with IP2Location LITE (free database) or IP2Location Database (commercial database). IP2Location database is updated monthly. You can get the latest database from www.ip2location.com (Commercial version) or lite.ip2location.com (Free version).
IP2Location Yii Extension (for Yii version 1.x)
For Yii version 2.x, please view the below section for the installation guide.
- Download the Yii Extension package.
- Extract this package to protected/extensions
- Download IP2Location free database for LITE version (free) or commercial version.
- Add following lines into main.php configuration file:
-
'components' => array( ... 'ip2location' => array( 'class' => 'application.extensions.ip2location.Geolocation', 'database' => 'C:\path\to\ip2location\IP2LOCATION-LITE-DB1.BIN', 'mode' => 'FILE_IO', ), ... ),
- You're done.
Use the following methods to retrieve geolocation information. If $ip not provided, value from CHttpRequest::getUserHostAddress() will be used.
$countryCode = Yii::app()->ip2location->getCountryCode($ip); $countryName = Yii::app()->ip2location->getCountryName($ip); $regionName = Yii::app()->ip2location->getRegionName($ip); $cityName = Yii::app()->ip2location->getCityName($ip); $latitude = Yii::app()->ip2location->getLatitude($ip); $longitude = Yii::app()->ip2location->getLongitude($ip); $isp = Yii::app()->ip2location->getISP($ip); $domainName = Yii::app()->ip2location->getDomainName($ip); $zipCode = Yii::app()->ip2location->getZIPCode($ip); $timeZone = Yii::app()->ip2location->getTimeZone($ip); $netSpeed = Yii::app()->ip2location->getNetSpeed($ip); $iddCode = Yii::app()->ip2location->getIDDCode($ip); $areaCode = Yii::app()->ip2location->getAreaCode($ip); $weatherStationCode = Yii::app()->ip2location->getWeatherStationCode($ip); $weatherStationName = Yii::app()->ip2location->getWeatherStationName($ip); $mcc = Yii::app()->ip2location->getMCC($ip); $mnc = Yii::app()->ip2location->getMNC($ip); $mobileCarrierName = Yii::app()->ip2location->getMobileCarrierName($ip); $elevation = Yii::app()->ip2location->getElevation($ip); $usageType = Yii::app()->ip2location->getUsageType($ip);
Inside the Yii 2.0 installation guide, it's recommended to use the composer for the installation. Thefore, in the below tutorials, we assume that you already have the composer installed in your environment.
Install the IP2Location PHP library using composer.
php composer.phar require ip2location/ip2location-php:8.*Or, add the following line into the require section and execute
php composer.phar update
"ip2location/ip2location-php": "8.*"
Use the following methods to retrieve geolocation information.
/* Cache whole database into system memory and share among other scripts & websites WARNING: Please make sure your system have sufficient RAM to enable this feature */ // $db = new \IP2Location\Database('path_to/IP-COUNTRY-SAMPLE.BIN', \IP2Location\Database::MEMORY_CACHE); /* Cache the database into memory to accelerate lookup speed WARNING: Please make sure your system have sufficient RAM to enable this feature */ // $db = new \IP2Location\Database('path_to//IP-COUNTRY-SAMPLE.BIN', \IP2Location\Database::MEMORY_CACHE); /* Default file I/O lookup */ $db = new \IP2Location\Database('path_to/IP-COUNTRY-SAMPLE.BIN', \IP2Location\Database::FILE_IO); $records = $db->lookup('8.8.8.8', \IP2Location\Database::ALL); echo 'IP Number : ' . $records['ipNumber'] . "\n"; echo 'IP Version : ' . $records['ipVersion'] . "\n"; echo 'IP Address : ' . $records['ipAddress'] . "\n"; echo 'Country Code : ' . $records['countryCode'] . "\n"; echo 'Country Name : ' . $records['countryName'] . "\n"; echo 'Region Name : ' . $records['regionName'] . "\n"; echo 'City Name : ' . $records['cityName'] . "\n"; echo 'Latitude : ' . $records['latitude'] . "\n"; echo 'Longitude : ' . $records['longitude'] . "\n"; echo 'Area Code : ' . $records['areaCode'] . "\n"; echo 'IDD Code : ' . $records['iddCode'] . "\n"; echo 'Weather Station Code : ' . $records['weatherStationCode'] . "\n"; echo 'Weather Station Name : ' . $records['weatherStationName'] . "\n"; echo 'MCC : ' . $records['mcc'] . "\n"; echo 'MNC : ' . $records['mnc'] . "\n"; echo 'Mobile Carrier : ' . $records['mobileCarrierName'] . "\n"; echo 'Usage Type : ' . $records['usageType'] . "\n"; echo 'Elevation : ' . $records['elevation'] . "\n"; echo 'Net Speed : ' . $records['netSpeed'] . "\n"; echo 'Time Zone : ' . $records['timeZone'] . "\n"; echo 'ZIP Code : ' . $records['zipCode'] . "\n"; echo 'Domain Name : ' . $records['domainName'] . "\n"; echo 'ISP Name : ' . $records['isp'] . "\n";
The following evaluation copy of the database file has limited range of IP address information compare to commercial. For evaluation purpose, you can query IP addresses from 0.0.0.0-99.255.255.255 (IPv4) or 2A04:0:0:0:0:0:0:0-2A04:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF (IPv6).
Sample Packages | IPv4 | IPv6 |
---|---|---|
IP2Location DB1 Sample Bin File | 669.26 kB | 350.84 kB |
IP2Location DB2 Sample Bin File | 38.57 MB | 378.66 kB |
IP2Location DB3 Sample Bin File | 18.03 MB | 363.65 kB |
IP2Location DB4 Sample Bin File | 53.59 MB | 388.27 kB |
IP2Location DB5 Sample Bin File | 27.76 MB | 370.18 kB |
IP2Location DB6 Sample Bin File | 63.11 MB | 393.44 kB |
IP2Location DB7 Sample Bin File | 59.80 MB | 398.64 kB |
IP2Location DB8 Sample Bin File | 69.34 MB | 403.55 kB |
IP2Location DB9 Sample Bin File | 32.20 MB | 373.45 kB |
IP2Location DB10 Sample Bin File | 74.79 MB | 408.65 kB |
IP2Location DB11 Sample Bin File | 32.83 MB | 374.17 kB |
IP2Location DB12 Sample Bin File | 75.32 MB | 409.59 kB |
IP2Location DB13 Sample Bin File | 31.65 MB | 371.65 kB |
IP2Location DB14 Sample Bin File | 76.59 MB | 410.29 kB |
IP2Location DB15 Sample Bin File | 35.67 MB | 378.07 kB |
IP2Location DB16 Sample Bin File | 81.13 MB | 415.93 kB |
IP2Location DB17 Sample Bin File | 38.47 MB | 380.09 kB |
IP2Location DB18 Sample Bin File | 88.19 MB | 425.95 kB |
IP2Location DB19 Sample Bin File | 72.11 MB | 404.88 kB |
IP2Location DB20 Sample Bin File | 90.45 MB | 427.07 kB |
IP2Location DB21 Sample Bin File | 38.98 MB | 380.72 kB |
IP2Location DB22 Sample Bin File | 94.52 MB | 430.11 kB |
IP2Location DB23 Sample Bin File | 72.56 MB | 405.90 kB |
IP2Location DB24 Sample Bin File | 95.42 MB | 431.79 kB |