How do you detect a website visitor's country

2019-01-21 06:45发布

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?

8条回答
The star\"
2楼-- · 2019-01-21 07:32

I would say that geotargetting is the only method that's even remotely reliable. But there are also cases where it doesn't help at all. I keep getting to sites that think I'm in France because my company's backbone is there and all Internet traffic goes through it.

The HTTP Accept Header is not enough to determine the user locale. It only tells you what the user selected as their language, which may have nothing to do with where they are. More on this here.

查看更多
SAY GOODBYE
3楼-- · 2019-01-21 07:34

Simply we can use Hostip API

<?php $country_code = file_get_contents("http://api.hostip.info/country.php"); <br/>if($country_code == "US"){ echo "You Are USA"; } <br/>else{ echo "You Are Not USA";} ?>

All Country codes are here.. http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

查看更多
登录 后发表回答