在语言检测期间重定向循环

时间:2013-11-11 03:06:30

标签: php http redirect infinite-loop

人,

我正在使用基于HTTP_ACCEPT_LANGUAGE的函数,当我测试时,浏览器会显示重定向循环。我调查了其他帖子中提供的所有解决方案但不起作用

更新:我首先尝试使用httaccess检测语言并使用正确的页面和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;
        }
    }
    }
   }

2 个答案:

答案 0 :(得分:0)

您可能正在从index.php页面调用detectBrowserLanguage。确保在URL中传递Lang参数时不调用该函数。

您可以使用

进行检查
isset($_GET['lang'])

答案 1 :(得分:0)

如果页面已经是检测到的语言,我已经添加了一个突破循环的方法。注意:请参阅我对您问题的评论。

$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;
        }
    }

}

参考:

  1. W3 Accept-Language RFC
  2. Language Tags