This extension is a port of the Net_GeoIP PEAR package for Yii to get a geographical location given an IP address.
Made some adjustments to make it independent of the PEAR package. Basically it's just a proxy module that uses the library developed ported by Hans Lellelid in the PEAR package.
protected/extensions'components' => array( ... 'geoip' => array( 'class' => 'application.extensions.geoip.CGeoIP', // specify filename location for the corresponding database 'filename' => 'C:\path\to\GeoIP\GeoLiteCity.dat', // Choose MEMORY_CACHE or STANDARD mode 'mode' => 'STANDARD', ), ... ),
See the following code example:
$location = Yii::app()->geoip->lookupLocation(); // with IP in google.com $location = Yii::app()->geoip->lookupLocation('209.85.135.104'); $countryCode = Yii::app()->geoip->lookupCountryCode(); $countryName = Yii::app()->geoip->lookupCountryName(); $org = Yii::app()->geoip->lookupOrg(); $regionCode = Yii::app()->geoip->lookupRegion(); // Location attributes: $location->countryCode $location->countryCode3 $location->countryName $location->region $location->regionName $location->city $location->postalCode $location->latitude $location->longitude $location->areaCode $location->dmaCode
Notes:
Methods lookupOrg and lookupRegion require specific databases (only paid versions available from MaxMind).
Total 1 comment
Works great, thanks!
I found that the "MEMORY_CACHE" was not a good idea as it loads the entire database into memory, and it is then destroyed after the script ends. So it's really much slower and a lot more memory consuming (35MB instead of 5MB).
Instead, I am using a little caching as follows:
Leave a comment
Please login to leave your comment.