2017-04-10 52 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」。然後確保項目中存在與該值相對應的子域... –