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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With