Edward Posted November 14, 2014 Share Posted November 14, 2014 If a user is surfing your website, how is the best way to get a users two letter country code e.g "US" if we don't know where they are located? I found the following options so far: http://php.net/manual/en/book.geoip.php http://ipinfo.io/developers https://developers.google.com/maps/documentation/geocoding/ My website will require numerous API requests for this info so i am looking for a sensible solution. Edward. Link to comment Share on other sites More sharing options...
HartleySan Posted November 14, 2014 Share Posted November 14, 2014 If they're on a device that supports geolocation, use the JS Geolocation API. If they're not (i.e., most desktop PCs), you can approximate their location by their IP address. (Although it won't be as accurate as geolocation, you should be able to get their country without problems.) 1 Link to comment Share on other sites More sharing options...
Edward Posted November 15, 2014 Author Share Posted November 15, 2014 Thanks for the fast response. I need such a request for such a page like eBay has for example http://www.ebay.com/sch/i.html?_from=R40&_trksid=p4712.m570.l1313.TR0.TRC0.H0.Xlarry+ullman&_nkw=larry+ullman&_sacat=0 This is their listings page. If i know where a user is from by either locating their country or using their signed in account info, i could add data to item listings specific to them for example if the seller offered international shipping or had free shipping available. There also has to be a buffer just incase i can't find their location and then I could just provide default data for a particular item. I really need the country to be picked up so it will work with my PHP script before the javascript even loads so i can perform specific page rendering. Obviously if i do have to trigger an API request i wouldn't want to repeat it everytime the page had to be reloaded, so I would either need to issue the user with a Cookie or possibly save the IP's country from the proceeding API request to the database to later act as a IP country look up table. Also i am unsure of what you mean by this? (If they're not (i.e., most desktop PCs), you can approximate their location by their IP address.) How can i find a users country via a IP address, i would need a lookup table or have to make a API request but from where exactly? Link to comment Share on other sites More sharing options...
HartleySan Posted November 15, 2014 Share Posted November 15, 2014 Yes, for an IP address lookup, you would either need to hit up an API (the preferred option, since it's always up to date) or have a database for referencing. Here's one such API you could use for IP address lookups: http://ipinfodb.com/ip_location_api.php The rest of what you need all sounds like business logic for your site and is pretty straightforward, so I'll let you handle all of that. 1 Link to comment Share on other sites More sharing options...
Edward Posted November 16, 2014 Author Share Posted November 16, 2014 Thanks for the link, i came across this one before actually from StackOverFlow while running a search. Thanks for your help. Edward, Link to comment Share on other sites More sharing options...
HartleySan Posted November 16, 2014 Share Posted November 16, 2014 Yeah, good luck with your task. Link to comment Share on other sites More sharing options...
Recommended Posts