您如何检测网站访问者的国家/地区(特别是美国与否)? [英] How do you detect a website visitor's country (Specifically, US or not)?

查看:51
本文介绍了您如何检测网站访问者的国家/地区(特别是美国与否)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为访问我网站的美国和非美国访问者显示不同的链接.这只是为了方便起见,所以我不是在寻找超高的准确性,而且安全或欺骗不是问题.

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.

我正在考虑使用 JavaScript 来获取用户的时区,但这似乎只给出了偏移量,因此加拿大、墨西哥和南美的用户将具有与美国用户相同的价值.

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.

除了获取 IP 地址并进行查找之外,还有其他可用的 JavaScript 或 PHP 信息吗?

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?

推荐答案

有一些免费服务可以让您从客户端进行基于国家和 ip 的地理定位.

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

我用过wipmania免费的JSONP服务,用起来真的很简单:

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 + 
          '
Longitude: ' + data.longitude + 
          '
Country: ' + data.address.country); 
  }
</script>
<script src="http://api.wipmania.com/jsonp?callback=jsonpCallback"
        type="text/javascript"></script>

或者,如果您使用支持 JSONP 的框架,例如 jQuery,您可以:

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 + 
        '
Longitude: ' + data.longitude + 
        '
Country: ' + data.address.country); 
});

检查上面运行的代码段这里.

Check the above snippet running here.

这篇关于您如何检测网站访问者的国家/地区(特别是美国与否)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆