Googlebot 收到错误

发布于 2024-09-19 08:34:29 字数 5019 浏览 6 评论 0 原文

我有以下代码作为我的多语言网站的index.php。每种可用语言都有一个子目录。

<?php


if (isset($_POST['URL']) && strlen($_POST['URL']) == 3) {
         header("location: ".$_POST[URL]);
}
else {

    function lixlpixel_get_env_var($Var)
    {
         if(empty($GLOBALS[$Var]))
         {
             $GLOBALS[$Var]=(!empty($GLOBALS['_SERVER'][$Var]))?
             $GLOBALS['_SERVER'][$Var] : (!empty($GLOBALS['HTTP_SERVER_VARS'][$Var])) ? $GLOBALS['HTTP_SERVER_VARS'][$Var]:'';
         }
    }

    function lixlpixel_detect_lang()
    {
         // Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
         lixlpixel_get_env_var('HTTP_ACCEPT_LANGUAGE');
         lixlpixel_get_env_var('HTTP_USER_AGENT');

         $_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']);
         $_UA=strtolower($GLOBALS['HTTP_USER_AGENT']);

         // Try to detect Primary language if several languages are accepted.
         foreach($GLOBALS['_LANG'] as $K)
         {
             if(strpos($_AL, $K)===0)
             return $K;
         }

         // Try to detect any language if not yet detected.
         foreach($GLOBALS['_LANG'] as $K)
         {
             if(strpos($_AL, $K)!==false)
             return $K;
         }
         foreach($GLOBALS['_LANG'] as $K)
         {
             if(preg_match("/[[( ]{$K}[;,_-)]/",$_UA))
             return $K;
         }

         // Return default language if language is not yet detected.
         return $GLOBALS['_DLANG'];
    }

    // Define default language.
    $GLOBALS['_DLANG']='en';

    // Define all available languages.
    // WARNING: uncomment all available languages

    $GLOBALS['_LANG'] = array(
    'en', // english.
    'es', // spanish.
    'fr', // french.
    );

    /*
    $GLOBALS['_LANG'] = array(
    'af', // afrikaans.
    'ar', // arabic.
    'bg', // bulgarian.
    'ca', // catalan.
    'cs', // czech.
    'da', // danish.
    'de', // german.
    'el', // greek.
    'en', // english.
    'es', // spanish.
    'et', // estonian.
    'fi', // finnish.
    'fr', // french.
    'gl', // galician.
    'he', // hebrew.
    'hi', // hindi.
    'hr', // croatian.
    'hu', // hungarian.
    'id', // indonesian.
    'it', // italian.
    'ja', // japanese.
    'ko', // korean.
    'ka', // georgian.
    'lt', // lithuanian.
    'lv', // latvian.
    'ms', // malay.
    'nl', // dutch.
    'no', // norwegian.
    'pl', // polish.
    'pt', // portuguese.
    'ro', // romanian.
    'ru', // russian.
    'sk', // slovak.
    'sl', // slovenian.
    'sq', // albanian.
    'sr', // serbian.
    'sv', // swedish.
    'th', // thai.
    'tr', // turkish.
    'uk', // ukrainian.
    'zh' // chinese.
    );
    */

    // Redirect to the correct location.


    header('location: /'.lixlpixel_detect_lang());
    //header('location: http://www.your_site.com/index_'.lixlpixel_detect_lang().'.php'); // Example Implementation
    echo 'The Language detected is: '.lixlpixel_detect_lang(); // For Demonstration

}

?>

问题是,虽然在用户浏览器中这工作得很好,但对于搜索引擎(如 Googlebot),会抛出以下错误:

    <br />
<b>Warning</b>:  preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>:  preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>:  preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /index.php:41) in <b>/index.php</b> on line <b>110</b><br />
<br />
<b>Warning</b>:  preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>:  preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>:  preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
The Language detected is: en

我已经尝试过错误处理,但我不是 PHP 程序员,我是 CF 程序员,所以我真的需要一些帮助!

I have the following code as my index.php for my multi-language website. Each available language has a subdirectory.

<?php


if (isset($_POST['URL']) && strlen($_POST['URL']) == 3) {
         header("location: ".$_POST[URL]);
}
else {

    function lixlpixel_get_env_var($Var)
    {
         if(empty($GLOBALS[$Var]))
         {
             $GLOBALS[$Var]=(!empty($GLOBALS['_SERVER'][$Var]))?
             $GLOBALS['_SERVER'][$Var] : (!empty($GLOBALS['HTTP_SERVER_VARS'][$Var])) ? $GLOBALS['HTTP_SERVER_VARS'][$Var]:'';
         }
    }

    function lixlpixel_detect_lang()
    {
         // Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
         lixlpixel_get_env_var('HTTP_ACCEPT_LANGUAGE');
         lixlpixel_get_env_var('HTTP_USER_AGENT');

         $_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']);
         $_UA=strtolower($GLOBALS['HTTP_USER_AGENT']);

         // Try to detect Primary language if several languages are accepted.
         foreach($GLOBALS['_LANG'] as $K)
         {
             if(strpos($_AL, $K)===0)
             return $K;
         }

         // Try to detect any language if not yet detected.
         foreach($GLOBALS['_LANG'] as $K)
         {
             if(strpos($_AL, $K)!==false)
             return $K;
         }
         foreach($GLOBALS['_LANG'] as $K)
         {
             if(preg_match("/[[( ]{$K}[;,_-)]/",$_UA))
             return $K;
         }

         // Return default language if language is not yet detected.
         return $GLOBALS['_DLANG'];
    }

    // Define default language.
    $GLOBALS['_DLANG']='en';

    // Define all available languages.
    // WARNING: uncomment all available languages

    $GLOBALS['_LANG'] = array(
    'en', // english.
    'es', // spanish.
    'fr', // french.
    );

    /*
    $GLOBALS['_LANG'] = array(
    'af', // afrikaans.
    'ar', // arabic.
    'bg', // bulgarian.
    'ca', // catalan.
    'cs', // czech.
    'da', // danish.
    'de', // german.
    'el', // greek.
    'en', // english.
    'es', // spanish.
    'et', // estonian.
    'fi', // finnish.
    'fr', // french.
    'gl', // galician.
    'he', // hebrew.
    'hi', // hindi.
    'hr', // croatian.
    'hu', // hungarian.
    'id', // indonesian.
    'it', // italian.
    'ja', // japanese.
    'ko', // korean.
    'ka', // georgian.
    'lt', // lithuanian.
    'lv', // latvian.
    'ms', // malay.
    'nl', // dutch.
    'no', // norwegian.
    'pl', // polish.
    'pt', // portuguese.
    'ro', // romanian.
    'ru', // russian.
    'sk', // slovak.
    'sl', // slovenian.
    'sq', // albanian.
    'sr', // serbian.
    'sv', // swedish.
    'th', // thai.
    'tr', // turkish.
    'uk', // ukrainian.
    'zh' // chinese.
    );
    */

    // Redirect to the correct location.


    header('location: /'.lixlpixel_detect_lang());
    //header('location: http://www.your_site.com/index_'.lixlpixel_detect_lang().'.php'); // Example Implementation
    echo 'The Language detected is: '.lixlpixel_detect_lang(); // For Demonstration

}

?>

The problem is that although in user browsers this works perfectly, with search engines (like Googlebot) the following error is thrown:

    <br />
<b>Warning</b>:  preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>:  preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>:  preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /index.php:41) in <b>/index.php</b> on line <b>110</b><br />
<br />
<b>Warning</b>:  preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>:  preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>:  preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
The Language detected is: en

I have tried error handling but I'm not a PHP programmer, I'm a CF progammer, so I really needs some help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风吹 2024-09-26 08:34:29

字符类内,-表示一个范围。在这种情况下,[;,_-)] 中的 _-) 被解释为范围(_ 之间的每个字符)< /code> 包括)。但 _ (0x95) 在 ) (0x28) 之后,因此 _-) 是无效范围。

如果您指的是三个单独的字符 _-),请转义 -

[;,_\-)]

除此之外, 接受语言是加权值列表(请参阅q参数)。这意味着仅出现特定语言标签并不一定意味着它是最首选的语言。可能有些语言更受青睐(q 值较高),甚至根本不被接受(即 q=0)。

因此,您不应该仅仅查找特定语言标签的出现,而应该更好地解析列表并找到首选语言和可用语言的最佳匹配。

Inside a character class, the - denotes a range. In this case _-) in [;,_-)] is interpreted as range (every character between _ and ) inclusive). But _ (0x95) is after ) (0x28), so _-) is an invalid range.

If you mean the three individual characters _, -, and ), escape the -:

[;,_\-)]

Apart from that, Accept-Language is a list of weighted values (see the q parameter). That means just the occurrence of a particular language tag does not necessarily mean that it’s the most preferred language. There might be languages that are more preferred (higher q value) or even not accepted at all (i.e. q=0).

So instead of just looking for the occurrence of a particular language tag, you should better parse the list and find the best match of preferred languages and available languages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文