We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 964
    • 11 Posts
    Hi,

    I thought I would post on how to use MaxMind geo-IP with MODx in order to identify which country your users are coming from and act accordingly.

    Here are the steps:

    1. DL all of the MaxMind PHP library files from http://geolite.maxmind.com/download/geoip/api/php/

    2. Store them in your library path (e.g. /var/www/libraries/MaxMind)

    3. DL the latest MaxMind geo-IP database from http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

    4. Extract the GeoIP.dat database file to /var/www/libraries/MaxMind/data/GeoIP.dat (or whatever your library path is)

    5. Now create a new snippet called maxmind.inc.php in a folder called maxmind with the following contents:

    <?php
    /**
     * This is a MaxMind loader for MODx Evolution
     * Author: Alex Dean (@alexatkeplar http://www.keplarllp.com)
     */
    
    define('LIBRARY_PATH', '/var/www/libraries/'); // Update as appropriate
    
    // Include the required PHP file
    require_once LIBRARY_PATH . 'MaxMind/geoip.inc';
    
    // Convenience function to return the path to the MaxMind geo-IP data
    function getMaxMindDataFile() {
        return LIBRARY_PATH . "MaxMind/data/GeoIP.dat";
    }
    


    6. Now to get the two letter country code for your site visitor, use the following code in a MODx snippet:

    <?php
    require_once($modx->config['base_path'] . 'assets/snippets/maxmind/maxmind.inc.php');
    
    // First get the country code using MaxMind geo-IP lookup
    $mm = geoip_open(getMaxMindDataFile(), GEOIP_STANDARD);
    $ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
    $countryCode = geoip_country_code_by_addr($mm, $ip);
    geoip_close($mm);
    
    echo $countryCode; // 'GB', 'US' or similar
    
    


    7. Final step: setup a monthly cron job to fetch the latest geo-IP database from MaxMind (as the IP->geography mapping gets regularly updated).

    I think that’s it! You can use a proxy service to test if the geo-IP is working. Hope you find this useful.
      • 964
      • 11 Posts
      I hope people are finding the above post useful. Just a quick follow-up to say that I’ve written more on why geo-location with MaxMind is useful here http://www.keplarllp.com/blog/2010/07/internationalising-your-e-commerce-site-with-maxmind-customer-geo-location.

      Any questions, please let me know!
        • 32718
        • 2 Posts
        Nice tip, I was going to implement MaxMind on my site by I found out about google’s api.

        No database updating needed either.

        On the bottom of this page:
        http://code.google.com/apis/ajax/documentation/

        You can get users country code with ’google.loader.ClientLocation.address.country_code’ call.

        Not sure how accurate it is though...
          • 25273
          • 13 Posts
          alexatkeplar, many thanks for posting this guide to using geo_IP with MODx.

          I have tried to implement this on our website and find I am having the following problem.

          Everytime I run these snippets through a proxy server the ip address picked up is my own ip address. I know the proxy server is operating correctly because if I look up my ip address via the proxy I get the correct proxy ip.

          Any ideas what might be happening?



          MODx version 1.0.3
          Version codename rev 6653
          phpInfo() View
          Access permissions Enabled
          Server Time 12:04:26
          Local Time 12:04:26
          Server offset 0 h
          Database server 127.0.0.1
          Database Version 5.0.92-community-log
            • 25273
            • 13 Posts
            Skip that last post, I have just realised that the snippets are running from the cache rather than running afresh for each user.



            Quote from: Robert_J at Jun 28, 2011, 06:06 AM

            alexatkeplar, many thanks for posting this guide to using geo_IP with MODx.

            I have tried to implement this on our website and find I am having the following problem.

            Everytime I run these snippets through a proxy server the ip address picked up is my own ip address. I know the proxy server is operating correctly because if I look up my ip address via the proxy I get the correct proxy ip.

            Any ideas what might be happening?



            MODx version 1.0.3
            Version codename rev 6653
            phpInfo() View
            Access permissions Enabled
            Server Time 12:04:26
            Local Time 12:04:26
            Server offset 0 h
            Database server 127.0.0.1
            Database Version 5.0.92-community-log
              • 24074
              • 81 Posts
              i don’t know, if Geo tracking is primary concern here, why wouldn’t one use Google Analytics?
              I think one c an’t get better Geo tracking than that.

              maybe i misunderstood the deal, sorry if i did.
                Radio sucks!
                • 9130
                • 171 Posts
                Thanks, works great for me.

                Quote from: purpler at Jun 28, 2011, 11:50 AM

                i don’t know, if Geo tracking is primary concern here, why wouldn’t one use Google Analytics?

                This is not just for tracking, its useful when you want to show different content to different users depending on their country.

                  • 44920
                  • 48 Posts
                  Hi all,

                  Apologize for re-opening the old thread but it is pretty related - I wonder if anybody tried to use MaxMind GeoIP2 in MODX.

                  I've tried GeoIP2-php but ended up on PHP parse error complaining on unexpected T_USE. I understood that MODX snippet are processed like a functions and one can't use:
                  use GeoIp2\Database\Reader;

                  within snippet (which seems to be handled as function).

                  I'm not sure how to sort this one.
                  Please advise if you are more familiar in using namespaces or have some experience with geolocation with MODX

                  Thank you in advance [ed. note: krajicek last edited this post 12 years, 1 month ago.]
                    MODX Revolution 2.2.8-pl (advanced)

                    Extras:
                    archivist 1.2.4 | Articles 1.7.1 | Babel 2.2.5 | getpage 1.2.3 | getresources 1.6.0 | quip 2.3.3 | SimpleSearch 1.6.1 | taglister 1.1.7 | TinyMCE 4.3.3 | Wayfinder 2.3.3

                    PHP Version 5.6.20-0
                    MySQL Version 5.5.31