Visitor Country

Hello! You are from: United States (US) and your IP is: 54.197.171.109.

Visitor Country is a WordPress plugin that adds support for displaying (using shortcodes) or enquiring (using PHP or JS) the visitor’s country.

It uses MaxMind’s GeoIP data file, which is around 1.4 MB in size and is included with the plugin. The file is free, updated every month, and has the claimed accuracy of 99.5%;

Git repository is here.

Download version 1.1

The plugin retrieves:

  • The visitor’s IP
  • The visitor’s country name
  • The visitor’s country code (2 letter ISO 3166)

The shortcodes are:

  • [VisitorCountry-IP]
  • [VisitorCountry-Code]
  • [VisitorCountry-Name]

The javascript variables are:

  • VisitorCountry.ip
  • VisitorCountry.code
  • VisitorCountry.name

And the PHP functions:

global $VisitorCountry;
if ( isset($VisitorCountry) )
{
    echo $VisitorCountry->GetIP();
    echo $VisitorCountry->GetCode();
    echo $VisitorCountry->GetName();
}

This product includes GeoLite data created by MaxMind, available from http://www.maxmind.com/app/geolitecountry.

 

15 Comments

  1. I’m behind work proxy and see on this page:
    “Hello! You are from: () and your IP is: 192.168.3.30.”

    You need adjust your code:

    // Loads the visitor data
    function LoadVisitorData()
    {
    // Get the path of the plugin
    $this->mPluginDir = plugin_dir_path( __FILE__ );
    $this->mPluginUrl = plugin_dir_url ( __FILE__ );

    // Include MaxMind’s API
    include( $this->mPluginDir . ‘geoip.inc’);

    // Get the visitor IP
    $iIp = $_SERVER[ 'REMOTE_ADDR' ];
    if ( !empty( $_SERVER[ 'HTTP_CLIENT_IP' ] ) )
    {
    // check ip from share internet
    $iIp = $_SERVER[ 'HTTP_CLIENT_IP' ];
    } elseif ( !empty( $_SERVER[ 'HTTP_X_FORWARDED_FOR'] ) )
    {
    // to check ip is pass from proxy
    $iIp = $_SERVER[ 'HTTP_X_FORWARDED_FOR' ];
    }
    $iIpList = explode(“,”, $iIp);
    foreach($iIpList as $ip){
    if(substr($ip,0,8) !== “192.168.”){
    $this->mIP=$ip;
    break;
    }
    }

    // Connect to MaxMind’s GeoIP
    $iGeoIP = geoip_open( $this->mPluginDir . ‘GeoIP.dat’, GEOIP_STANDARD);

    // Get the country id
    $iCountryID = geoip_country_id_by_addr( $iGeoIP, $this->mIP );

    if ( $iCountryID !== false ) {
    // Lookup country code and name
    $this->mCode = $iGeoIP->GEOIP_COUNTRY_CODES[$iCountryID];
    $this->mName = $iGeoIP->GEOIP_COUNTRY_NAMES[$iCountryID];
    }

    // Close MaxMind’s connection
    geoip_close($iGeoIP);
    }

    • Thanks,

      This has been updated in version 1.1;

      Won’t work for LAN IPs of 127.0.X.X, but we’ll fix it if anyone complains.

      Thanks!

  2. How can i show the city instead of country?

    • As the FAQ of the plugin say:

      Can the plugin retrieve the city of the visitor, what about latitude and longtitude information?
      No. The accuracy of MaxMind’s GeoLite City database is far from perfect; combined with a much bigger database, there’s no much point supporting it in this plugin. Shall there be such a demand, a separate city plugin will be written.

  3. I love this plug-in. Combined with a couple of other tools, including a good translation plug-in and you have a site capable of selling to anywhere in the world.

    Been using this for ages, and it’s top.

    Slighty disappointed that the latest version no longer works in widgets or the side bar.
    Why is that?

    • I’m looking into the widget business – something has change in the latest version of wordpress that cause the widget to run no more. Should be 2 weeks or so…

  4. It would be nice to have the City also :) just an idea because i would like to use it in my site Raporteri and display news from that city. Bye and thank you in advance

    • See the as to why city is not supported (I have tested it from London, and it returned a different city – so clearly the city database is far from perfect).

  5. Hi, I have installed the plugin in my site, should I update the MaxMind’s GeoIP data file manually each monto?
    Kind Regards,

    • I mean each month…

    • The thing is that updating the database means I have to bump the plugin version, and doing this each month would be somewhat confusing. So currently I update the database every time a real update is needed.

      Please feel free to update the database manually each month by replacing the dat file, per the instructions on the plugin’s .

  6. Hi, I am trying to implement this inside another plugin to only show a popup for a specific country, but can’t get the php tests to work. (Template files are OK)

    Is there something I need to do differently to access this inside a plugin?

    Cheers

  7. We ran into an issue with one of our servers. We believe in your LoadVisitorData() function, the $_SERVER variable ‘HTTP_X_FORWARD_FOR’ should be ‘HTTP_X_FORWARDED_FOR’ ?

    Also, some servers will use HTTP_CLIENT_IP. And some configurations will return a list of IP addresses. One of our senior developers submitted this snippet, which would solve all these problems;

    // Get the visitor IP
    $ip=$_SERVER['REMOTE_ADDR'];
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {               // check ip from share internet
        $ip=$_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {   // to check ip is pass from proxy
        $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    $ips = explode(",", $ip);
    $this->mIP=$ips[0];

    Please can this or similar be incorporated in your next release?

    Many thanks!

    • Implemented in 1.0.1

  8. Excuse my ignorance, but I have installed your plugin, but nothing shows up. What have I not done?
    Yours, Martin Hayward.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>