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

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

问题描述

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

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.

Are有没有其他信息可用于JavaScript或PHP,没有抓取IP地址和进行查找,以确定这个?

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服务,它真的是s实现使用:

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>

或者如果你使用支持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 + 
        '\nLongitude: ' + data.longitude + 
        '\nCountry: ' + data.address.country); 
});

检查上面的代码片段这里

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

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