zen of coding

Very hacky way to get client’s country code based on IP

You’d think that by now there should be an easier way to accomplish something like this.
(i.e. Send a client IP to some service, and get their country or other location information).

All my research just pointed to some some API’s, complicated components and way to much extra leg work to achieve something seemingly simple.

Note, I need to take care of this on the server side, thus I couldn’t fully leverage google’s JSAPI.

Yet, I could do the following:

public function getClientCountry() {
    App::import('Core', 'HttpSocket');
    $HttpSocket = new HttpSocket();
    $response = preg_split('/"country_code":"/', $HttpSocket->get('http://www.google.com/jsapi?'));
    return substr($response[1], 0, 2);
  }

Simply load the above URL in your browser to see the actual response…
Once I get the full response back, I hack my way to “extract” the value. Blah.

p.s. If someone has better alternatives, please share. Thanks ;)

  • http://api.hostip.info/get_html.php?ip=210.151.10.0

    much smaller data transfer and the full name, code and city is available

  • teknoid

    @dogmatic69

    Thank you for sharing, they do have a much simpler service. However, I found it to be unreliable… with my initial testing it didn’t decode my office or home IP’s.
    And I don’t live in some very obscure area, nor do I use some random ISP.

    Also, from their site: “If you’re looking for a commercial option, this database [MaxMind] maintains a great level of accuracy.”

    But again, thank you for sharing hopefully this will be helpful to some.

  • David

    Haven’t tried this myself, but came across it while browsing and bookmarked it. Take a look: http://ipligence.com/free-ip-database

  • Dan
  • Toumaz

    This is what I use and recommend: http://www.maxmind.com/app/geoip_country

  • You can try using a web-service like Quova[1]. It has a free dev version, but if you’re looking to use it in a larger app you may have to pay.

    [1] http://www.quova.com/what/products/

  • Cesar

    If you do this from server, the i.p. sent wouldn’t be the server I.P.?

  • Pingback: Tweets that mention Very hacky way to get client’s country code based on IP | nuts and bolts of cakephp -- Topsy.com()

  • teknoid

    @All

    Thank you for all the suggestions. As mentioned they are quite helpful, but most require complex integration. I would much prefer to use something like dogmatic posted… but again not very reliable.

    @Cesar

    Loading google api example above will give you the client side IP. Also if you wish to implement another solution use PHP’s “REMOTE_ADDR” (which should give you client’s IP, although it is also not 100% accurate).

    • Toby

      I think I’m being thick, but I don’t get how your example will get the client’s IP, am I missing something?

  • teknoid

    @Toby

    Just try to load the URL in the example directly, you’ll see that google will return the client location, you can try from a few servers if possible.

  • matt

    @teknoid
    It gets information for your own IP, but how do you get it for a client’s?

  • teknoid

    @matt

    Again, try it from different locations and see what you get.

  • I just finished working on a component to localize a client, leveraging YQL (if you don’t know it, it’s a wonderful way of getting virtually any info from the web, an API of API’s, an API endpoint, call it what you like), witch caching, and overriding the webservice call if location info is found in the URL.
    Willing to share, if anybody’s interested.

  • teknoid

    @Marek Stasikowski

    Thank you for sharing this info. Very useful.

  • Marcel

    Hi

    i use this http://www.utrace.de/api.php

    greets

    Marcel

  • David Henley

    I am beginning CakePHP (as of September 2010) and have found this simple google code to add to your view to see a user’s location. Would be great if it could show IP though.

    if (google.loader.ClientLocation != null) {
    document.write(“Your Location Is: ” + google.loader.ClientLocation.address.city + “, ” + google.loader.ClientLocation.address.region);
    } else {
    document.write(“Your Location Was Not Detected By Google Loader”);
    }

%d bloggers like this: