如何从特定国家/地区重定向用户

How do I redirect users from a specific country?

本文关键字:地区 重定向 用户 国家      更新时间:2023-09-26

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

我一直在谷歌上搜索一些可以做到这一点的脚本,但没有找到任何东西。有人可以指出我正确的方向吗?

理想情况下,我希望能够使用Javascript和/或HTML5实现它,但如果这是不可能的,请告诉我替代方案。

这里的

教程:地理方向

<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>

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

总结:

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

AletrNative 解决方案是使用 Web 服务 http://freegeoip.net/,但请求有每日限制。