2017-04-10 54 views

回答

0

有一个网站,提供一个给定的IP定位技术(实际上有很多,但我将只专注于这一个):http://ip-api.com/

可以使用他们为了提供信息,以重定向访问者相应的子域这样的:

$this_ip = $_SERVER["REMOTE_ADDR"]; 
$get_ip_info = file_get_contents("http://ip-api.com/json/".$this_ip); 
$ip_info = json_decode($get_ip_info); 
$countryCode = strtolower($ip_info->{"countryCode"}); 

header("Location: http://".$countryCode.".example.com");  

当然,也有许多国家,自实际上不可能保持为每一个子域,您可以使用“如果”语句来缩小重定向选项:

if($countryCode=="fr"){ //french 
    header("Location: http://fr.example.com"); 
}else 
if($countryCode=="de"){ //german 
    header("Location: http://de.example.com"); 
}else 
if($countryCode=="gr"){ //greek 
    header("Location: http://gr.example.com"); 
}else{ //universal 
    header("Location: http://www.example.com"); 
} 

希望这可以帮助你!

+0

它不工作,当我们将其重定向到子域时,网站没有打开。实际上 –

+0

如果您直接访问(例如)de.example.com,该网站可以正常工作,但是如果您使用我的脚本去那里不工作?你是这个意思吗? –

+0

首先尝试“回显”变量“countryCode”。然后确保项目中存在与该值相对应的子域... –