I have a site that was happily outputting variables based upon a user's IP address. I was using code that looked to see if a cookie for a user's region was already set and if not, looking up the user's country and then setting said cookie and the appropriate variables.

My code was in a PHP enabled template parsing on output and included the following…

$sql = 'SELECT country FROM exp_ip2nation WHERE ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'") 
list($country) = mysql_fetch_row(mysql_query($sql));

But since updating the site in question I find that EE now uses maxmind.com data instead of ip2nation.com, and that what was a single ip field is now 2: ip_range_low and ip_range_high. As a consequence my code no longer works.

Can anyone suggest how to fix my code or suggest a better way of achieving the same end?


I was just about to have a crack at a little plugin for this but it looks as though Andrew Weaver beat me to it. By the sound of it his plugin will do what you're after:

{exp:ajw_country} to display the 2 letter country code of the current IP

  • Thanks Dom. Looks promising. Will have a play with it tomorrow and report back. – Jonathan Schofield Feb 5 '13 at 23:42
  • Just tried ajw_country and it's not working for me. I infer it's because of the change to the data structure as mentioned above. I've commented as such on Andrew's Bitbucket commit: bitbucket.org/ajweaver/ajw_country/commits/e56469a15c7d – Jonathan Schofield Feb 6 '13 at 9:51
  • The addon is definitely designed to use the newer data structure as it references the Ip_to_nation_data class. It looks as though Andrew's helping you out so hopefully you'll be able to get it working. – Dom Stubbs Feb 6 '13 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.