2011-03-05 39 views
5

我需要檢測用戶國家並顯示其國家/地區的網站語言。 (土耳其人爲土耳其人,英語爲所有其他人)檢測用戶國家的最快方法

我該如何做這個最快的方式?性能對我很重要。我想找IPInfoDB' API,有沒有更好的選擇?

(我使用PHP)

+6

你幾乎肯定要使用,而不是試圖揣摩出用戶所在的瀏覽器的首選語言設置。 – geoffspear 2011-03-05 18:53:33

+3

就像@Wooble的評論 - 這不是一個好主意。我住在加拿大 - 我的首選語言是什麼?如果我來自魁北克省,可能是法國人,或者我可能是意大利人或德國人......我住的地方並不決定我的首選語言。如果你願意,你可以添加一個鏈接,讓用戶切換到你「想」他們想要的語言。例如:Préférezfrançais? – scunliffe 2011-03-05 18:56:49

+0

「我怎樣才能做到這一點最快的方式」是錯誤的。您應該每次訪問一次*設置一個cookie。請確保允許用戶覆蓋您的默認設置。 – bart 2011-03-05 20:02:18

回答

1

正如其他人指出的那樣,爲土耳其語檢查Accept-Language HTTP標題可能會更好。如果這是首選語言,請提供。否則服務英語。 這是some code

2

要做到這一點,我發現使用「GAE-IP到國家」最好的辦法庫:使用 https://github.com/andris9/GAE-IP-TO-COUNTRY/tree/master/src/ip_files

示例(您必須將「ip_files」目錄複製到服務器) :

function iptocountry($ip) {  
    $numbers = preg_split("/\./", $ip);  
    include("./ip_files/".$numbers[0].".php"); 
    $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);  
    foreach($ranges as $key => $value){ 
     if($key<=$code){ 
      if($ranges[$key][0]>=$code){$country=$ranges[$key][1];break;} 
     } 
    } 
    return $country; 
} 

$country=iptocountry($_SERVER['REMOTE_ADDR']); 
echo $country; 
0

我使用接受語言編碼的下一個東西是其他網友指出:

function GetAcceptedLangs() 
{ 
    $res=array(); 
    $a=getallheaders(); 
    if(isset($a["Accept-Language"])) 
    { 
     $aceptlangs=explode(",",str_replace(array(';','0','1','2','3','4','5','6','7','8','9','.',"q="),array(',','','','','','','','','','','','',""),$a["Accept-Language"])); 
     foreach($aceptlangs as $i=>$v) 
     { 
      if(trim($v)!="") 
       $res[]=trim($v); 
     } 
    } 
    return $res; 
} 

簡單

print_r(GetAcceptedLangs()); 

回報在我的情況:

Array ([0] => es-ES [1] => es [2] => en) 

您可以以後這樣定義一個數組來改變你的內部語言的值,例如:

$al={"ES-es"=>"es","es"=>"es","en"=>"en"......} 

它們是由已經排序用戶偏好。

如果陣列中不存在所有語言,則可以轉到網站的默認語言。如果瀏覽器不發送Accept-Language頭,這也是有效的。

另一個版本移除子區域值

function GetAcceptedLangs2() 
{ 
    $res=array(); 
    $a=getallheaders(); 
    if(isset($a["Accept-Language"])) 
    { 
     $aceptlangs=explode(",",str_replace(array(';','0','1','2','3','4','5','6','7','8','9','.',"q="),array(',','','','','','','','','','','','',""),$a["Accept-Language"])); 
     foreach($aceptlangs as $i=>$v) 
     { 
      $va=trim($v); 
      if(($pos=strpos($va,"-"))!==false) 
       $l=substr($va,0,$pos); 
      else 
       $l=$va; 
      if($l!="" && !isset($check[$l])) 
      { 
       $check[$l]=1; 
       $res[]=$l; 
      } 
     } 
    } 
    return $res; 
} 

它會在我的情況下返回

Array ([0] => es [1] => en) 
1

嘛的人誰可能在2017年訪問,這是一個解決這個極其簡單易用

<button class="btn dropdown-toggle" style="cursor:pointer;z-index:1000!important;margin-top:-67px;background:none!important;font-size:1.4em;" onclick="window.location.href='language'"> 
(a) <?php 
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { 
$ip = $_SERVER['HTTP_CLIENT_IP'];} 
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; 
} else { 
$ip = $_SERVER['REMOTE_ADDR']; 
} 
$url = "http://api.wipmania.com/".$ip; 
(h) $country = file_get_contents($url); //ISO code for Nigeria it is NG check your country ISO code 
?> 

<?php if ($country == ""){ 
echo "Country not found"; 
} else { ?> 
<script> 
var map = "<?php echo $country ?>"; 
$.ajax({ 
type : 'POST', 
url : 'http://www.mowemy.com/countryflag.php', 
data : {map: map}, 
success : function(r) { 
//console.log("success: " + r); 
$('#mumil').html(r); 
} }) 
</script> 
<?php } ?> 

<div id ="mumil" style="font-size:13px!important;margin-top:5px;"></div> 
</button> 

讓我把它寫下來字母A - H是腳本來檢測您的ISO的編號untry爲我的國家尼日利亞它是NG你可以谷歌搜索你的國家ISO號碼,用這個腳本它是自動檢測到的。然後,我創建了一個網頁,一些信息你火AJAX到該頁面,將其傳送回該國國旗的顏色和名稱簡單容易請致電JQUERY前AJAX來運行AJAX除非它不會工作古德納克

0

這裏是直截了當的方式,我喜歡用

<?php 
class GeoIp 
{ 
    protected $api = 'https://ipapi.co/%s/json/'; 

    protected $properties = []; 

    //------------------------------------------------------ 
    /** 
    * __get 
    * 
    * @access public 
    * - @param $key string 
    * - @return string/bool 
    */ 
    public function __get($key) 
    { 
     if(isset($this->properties[$key])) 
     { 
      return $this->properties[$key]; 
     } 

     return null; 
    } 

    //------------------------------------------------------ 
    /** 
    * request_geoip 
    * 
    * @access public 
    * - @return void 
    */ 
    public function request_geoip($ip) 
    { 
     $url = sprintf($this->api, $ip); 
     $data = $this->send_geoip_request($url); 
     $this->properties = json_decode($data, true); 
     //var_dump($this->properties); 
    } 

    //------------------------------------------------------ 
    /** 
    * send_geoip_request 
    * 
    * @access public 
    * - @param $url string 
    * - @return json 
    */ 
    public function send_geoip_request($url) 
    { 
     $loc_content = file_get_contents($url); 

     return $loc_content; 
    } 
} 

//You can access it like this: 
$geo = new GeoIp; 
$geo->request_geoip('foo'); 
echo $geo->country; 

?> 

訪問this website更多信息