[英]Redirect Loop during the language detection
人,
我正在使用基於HTTP_ACCEPT_LANGUAGE的功能,當我測試時,瀏覽器會顯示重定向循環。 我調查了其他帖子中提供的所有解決方案,但沒有用
更新:我首先嘗試使用httaccess來檢測語言,並使用正確的page和lang變量重定向到正確的頁面,但是我發現了相同的問題。 問題是我使用了兩個變量:page(確定用戶在哪個頁面中)和lang(取決於用戶的口頭語言)。 但是,這是用戶第一次到達該站點時:兩個變量都為null,因此其目的是檢測用戶的瀏覽器語言,然后將其重定向到適當的url,在我的情況下..... index.php?例如page = index&lang = en ...
我這樣做:
**init.php**
$supportedLangs = array (
'en-GB',
'en-US',
'es-ES',
'es-AR'
);
$languages = explode ( ',', $_SERVER ['HTTP_ACCEPT_LANGUAGE'] );
detectBrowserLanguage($languages,$supportedLangs);
**functions.php**
function detectBrowserLanguage($languages,$supportedLangs) {
foreach ( $languages as $lang ) {
if (in_array ( $lang, $supportedLangs )) {
// Set the page locale to the first supported language found
switch ($lang) {
case 'es-ES' :
header ( "Location: index.php?pagina=inicio&lang=es", TRUE, 301 );
exit ();
break;
case 'en-US' :
header ( "Location: index.php?pagina=inicio&lang=en", TRUE, 301 );
exit ();
break;
case 'en-GB' :
header ( "Location: index.php?pagina=inicio&lang=en", TRUE, 301 );
exit ();
break;
default :
header ( "Location: index.php?pagina=inicio&lang=es", TRUE, 301 );
exit ();
break;
}
}
}
}
您可能正在從index.php頁面調用detectBrowserLanguage。 確保在URL中傳遞Lang參數時未調用該函數。
你可以用
isset($_GET['lang'])
如果頁面已使用檢測到的語言,我已經添加了一種打破循環的方法。 注意:請參閱我對您問題的評論。
$supportedLangs = array (
'en-GB',
'en-US',
'es-ES',
'es-AR'
);
$languages = explode ( ',', $_SERVER ['HTTP_ACCEPT_LANGUAGE'] );
$currentLanguage='';
if(isset($_GET['lang'])){
$currentLanguage=$_GET['lang'];
}
detectBrowserLanguage($languages,$supportedLangs, currentLanguage);
功能
function detectBrowserLanguage($languages,$supportedLangs, $currentLanguage) {
foreach ( $languages as $lang ) {
if ($lang == $currentLanguage){
return; //If the page is already redirected just return.
}elseif (in_array ( $lang, $supportedLangs )) {
// Set the page locale to the first supported language found
switch ($lang) {
case 'es-ES' :
header ( "Location: index.php?pagina=inicio&lang=es", TRUE, 301 );
exit ();
break;
case 'en-US' :
header ( "Location: index.php?pagina=inicio&lang=en", TRUE, 301 );
exit ();
break;
case 'en-GB' :
header ( "Location: index.php?pagina=inicio&lang=en", TRUE, 301 );
exit ();
break;
default :
header ( "Location: index.php?pagina=inicio&lang=es", TRUE, 301 );
exit ();
break;
}
}
}
}
編輯:
您可以使用http_negotiate_language輕松完成此操作 。 這只是有關操作方法的指南。 我沒有對此進行測試(只需在此處輸入)
function detectBrowserLanguage($supportedLangs, $currentLanguage) {
$negotiated_language = http_negotiate_language($supportedLangs);
if ($currentLanguage != $negotiated_language){
switch ($negotiated_language) {
case 'es-ES' :
header ( "Location: index.php?pagina=inicio&lang=es", TRUE, 301 );
exit ();
break;
case 'en-US' :
header ( "Location: index.php?pagina=inicio&lang=en", TRUE, 301 );
exit ();
break;
case 'en-GB' :
header ( "Location: index.php?pagina=inicio&lang=en", TRUE, 301 );
exit ();
break;
default :
header ( "Location: index.php?pagina=inicio&lang=es", TRUE, 301 );
exit ();
break;
}
}
}
參考:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.