How to get user location data without using HTML5 or Google maps

You, AjaxHTML5Geolocation
Back

First of all, this might not be accurate location and this is more like a JS IP2Location concept.

I recently worked on a project that required to detect the user country and redirect to the proper website based on that, I'm on Drupal and I know there are contrib modules out there to do that. but we have Varnish enabled on our server so PHP won't execute at all. So I had to think about a better and easy solution for that.

I couldn't use HTML Geocoder because it requires user permission to get the current location. I couldn't use Google geolocater (I cannot rem why, I think they have deprecated their API).

So only solution I had was this, this is a tiny little hack to get the user location and couple of other information from appspot.com :).

    $.ajax({
        url: "http://ajaxhttpheaders.appspot.com",
        dataType: 'jsonp',
        success: function (headers) {
            country = headers\['X-Appengine-Country'\];
            if (country == 'US') {
                return;
            } else {
                // your code here
            }
        }
    });

Would you like to see the complete response object:

{ "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,\*/\*;q=0.8",
  "Accept-Language" : "en-US,en;q=0.8",
  "Alexatoolbar-Alx-Ns-Ph" : "AlexaToolbar/alxg-3.2",
  "Host" : "ajaxhttpheaders2.appspot.com",
  "User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10\_8\_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36",
  "X-Appengine-City" : "New York City",
  "X-Appengine-Citylatlong" : "79.861243, 6.927079",
  "X-Appengine-Country" : "US",
  "X-Appengine-Region" : "New York"
}
© Heshan Wanigasooriya.RSS

🍪 This site does not track you.