Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you detect a website visitor's country (Specifically, US or not)?

I need to show different links for US and non-US visitors to my site. This is for convenience only, so I am not looking for a super-high degree of accuracy, and security or spoofing are not a concern.

I know there are geotargeting services and lists, but this seems like overkill since I only need to determine (roughly) if the person is in the US or not.

I was thinking about using JavaScript to get the user's timezone, but this appears to only give the offset, so users in Canada, Mexico, and South America would have the same value as people in the US.

Are there any other bits of information available either in JavaScript, or PHP, short of grabbing the IP address and doing a lookup, to determine this?

like image 265
David Smith Avatar asked Jan 28 '10 16:01

David Smith


People also ask

How do you detect a website visitor's country?

To get the website visitor's location, I use the ip2c.org service that quickly resolves the visitor's IP address to their country. If you fetch the ip2c.org/self service, it returns the ISO code of the country of the computer that made the HTTP request.

How can I know the user of a country?

An easy way to get the users country is to read the HTTP_ACCEPT_LANGUAGE header. Show activity on this post. Any checking usualy go by IP and using some database of ipranges and to what country thay are assigned. This works in most cases unless the user uses some proxy or likewize.

How do I change my website content based on visitor country?

If you are running a ecommerce store and need to show products, prices and content based on a visitor's country, then the best approach is to create separate stores and route traffic to the correct store based on their location.


1 Answers

There are some free services out there that let you make country and ip-based geolocalization from the client-side.

I've used the wipmania free JSONP service, it's really simple to use:

<script type="text/javascript">   // plain JavaScript example   function jsonpCallback(data) {      alert('Latitude: ' + data.latitude +            '\nLongitude: ' + data.longitude +            '\nCountry: ' + data.address.country);    } </script> <script src="http://api.wipmania.com/jsonp?callback=jsonpCallback"         type="text/javascript"></script> 

Or if you use a framework that supports JSONP, like jQuery you can:

// jQuery example $.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) {    alert('Latitude: ' + data.latitude +          '\nLongitude: ' + data.longitude +          '\nCountry: ' + data.address.country);  }); 

Check the above snippet running here.

like image 195
Christian C. Salvadó Avatar answered Oct 08 '22 17:10

Christian C. Salvadó