Yii 1.1: egeoip

EGeoIp Extension uses geoPlugin free webservice to locate IP geo information

This extension allows you to easily integrate the geoPlugin webservice into your Yii applications.

To download and/or contribute to the extension, please use the github repository.

What is geoPlugin?

geoPlugin offers you, the webmaster, the ability to easily geo-localize your visitor down to the city they are in, know what currency they use and the up-to-date currency exchange rate of their currency versus yours.


Developed with Yii 1.1.6


Unpack the contents of the extension to your protected/extensions directory. Once you do that, the following is an example on how to use it:

$geoIp = new EGeoIP();
$geoIp->locate(''); // use your IP
echo 'Information regarding IP: <b>'.$geoIp->ip.'</b><br/>';
echo 'City: '.$geoIp->city.'<br>';
echo 'Region: '.$geoIp->region.'<br>';
echo 'Area Code: '.$geoIp->areaCode.'<br>';
echo 'DMA: '.$geoIp->dma.'<br>';
echo 'Country Code: '.$geoIp->countryCode.'<br>';
echo 'Country Name: '.$geoIp->countryName.'<br>';
echo 'Continent Code: '.$geoIp->continentCode.'<br>';
echo 'Latitude: '.$geoIp->latitude.'<br>';
echo 'Longitude: '.$geoIp->longitude.'<br>';
echo 'Currency Symbol: '.$geoIp->currencySymbol.'<br>';
echo 'Currency Code: '.$geoIp->currencyCode.'<br>';
echo 'Currency Converter: '.$geoIp->currencyConverter.'<br/>';
echo 'Converting $10.00 to '.$geoIp->currencyCode.': <b>'.$geoIp->currencyConvert(10).'</b><br/>';


Please make use of the forum post to report errors, requests, and suggestions. Let comments on this extension for coding hints.


Change Log

version 1.0

  • Initial Public Release

Total 7 comments

#12316 report it
mem at 2013/03/13 01:01pm
Got an error sunddenly

For some reason I got this error: unserialize() [function.unserialize]: Error at offset 0 of 172 bytes

Related to the curl access.

I solved by adding a else clause to the class method fetch:

} else { 
 return unserialize($response);

Now it's working.

If this is NOT the proper way of solving it, please advice.


#6583 report it
csalvato at 2012/01/19 10:42am

Thanks for the replies, Tony!

I also wanted to mention that I had a bit of a problem with the extension when checking to see if something was set. Because you use Unserialize() without var_export() on the response, the values entered into the map don't turn into "NULL" very well. In fact, I am not quite sure what is being stored in the variables.

For example, if you use an IP that is in the UK, for example, many times it won't produce a city or region, so you want to just display the country.

If there was no city found, when checking isset($geoIp->city), it will still return true. Same goes for empty( $geoIp->city ). To get around this in Yii, I have to use CHtml::encode() first to translate whatever is in there into a null string.

It's not a big deal, just a bit hackish and not intuitive. Thought you might want to know to increase the robustness of the extension. I couldn't figure out a way to change it myself to make it work well with CMap(), but maybe you do, as you are more experienced?

#6581 report it
Antonio Ramirez at 2012/01/19 10:31am

Good point, I should change it... Nevertheless, I have recently updated the extension to work with adapters (so people are free to add new ones to the library) that work with:

I found Ip2Nation the most accurate one... Will update it as soon as I have a bit of time though

PS: Thanks for sharing your thoughts!

#6473 report it
csalvato at 2012/01/12 11:08am

Yes, it is just a shortcut, but the isset() check makes it slightly more robust, in my opinion..and it makes Yii code a little more readable. That said, it really is a matter of preference. I made the change in my extension and am happy with it, but it is really up to the user. Either way, its a well-put-together extension.

That said, I wouldn't worry about finding a user's "real" IP address if they are behind a proxy. Most people behind proxies understand that this will alter where the internet "thinks" they are from, and actually prefer to see that they are not being traced back to their original location.

#6460 report it
Antonio Ramirez at 2012/01/12 05:01am

Yeah, thought about that but that function is just a shortcut to the same function.

// code inside Yii::app()->request->userHostAddress function
return isset($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:'';

I have been looking around for a good way to get the 'real' IP address of a visitor, just in case it comes behind proxies but I disliked it as it relies on headers who can be easily spoof.

function get_ip_address() {
        if (array_key_exists($key, $_SERVER) === true) {
            foreach (explode(',', $_SERVER[$key]) as $ip) {
                if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                    return $ip;
#6456 report it
csalvato at 2012/01/11 11:07pm
Small fix makes it 100% compliant with Yii best practices.

Nice work converting the class made by geoPlugin into a Yii compatible extension! I really like it so far.

Only thing I would correct is the fact that you use $_SERVER['REMOTE_ADDR'] on line 58 of the code. You should be using Yii::app()->request->userHostAddress, instead, to adhere to Yii best practices. Other than that, kudos!

#3091 report it
duplabe at 2011/03/15 11:45am

I think if the service is down or something go wrong with the curl/file_get_content call locate should return false instead true.

Leave a comment

Please to leave your comment.

Create extension