您如何检测网站访问者的国家/地区(特别是美国与否)?

How do you detect a website visitor#39;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.

我正在考虑使用 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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)