如何重定向来自特定国家/地区的用户?

How do I redirect users from a specific country?

我希望能够检测网页访问者是否位于美国,如果是,我想将他们重定向到其他页面。

我一直在谷歌搜索一些可以执行此操作的脚本,但没有找到任何内容。有人能指出我正确的方向吗?

理想情况下,我希望能够通过 Javascript and/or HTML5 实现它,但如果那不可能,请告诉我其他选择。

有一些脚本可以解决您的问题(在PHP):http://www.phptutorial.info/iptocountry/the_script.html

总结:

  1. $_SERVER['REMOTE_ADDR'] = 用于检测访问者 IP 地址的变量
  2. 在上面的页面上下载 IP 地址范围列表(您可以选择特定的国家/地区列表或下载所有国家/地区)- 每个国家/地区都有 IP 地址范围

另一种解决方案是使用网络服务http://freegeoip.net/,但每日请求有限制。

教程来自这里:GeoDirection

<script language="Javascript" src="http://gd.geobytes.com/gd?after=-1&variables=GeobytesLocationCode,GeobytesCode,GeobytesInternet"></script>
<script language="Javascript">
if(typeof(sGeobytesLocationCode)=="undefined"
   ||typeof(sGeobytesCode)=="undefined"
   ||typeof(sGeobytesInternet)=="undefined")
{
   // Something has gone wrong with the variables, so set them to some default value,
   // maybe set a error flag to check for later on.
   var sGeobytesLocationCode="unknown";
   var sGeobytesCode="unknown";
   var sGeobytesInternet="unknown";
}
if(sGeobytesLocationCode=="GRATATHE")
{
   // Visitors from Athens would go here
   window.open("enter Athens URL here");
}else if(sGeobytesCode=="AT")
{
   // Visitors from Attiki would go here
   window.open("enter Attiki URL here");
}else if(sGeobytesInternet=="GR")
{
   // Visitors from Greece would go here
   window.open("enter Greece URL here");
}
</script>