未定义的服务器变量错误(在谷歌缓存中)

时间:2015-12-24 19:53:59

标签: php

我的网站使用一个类根据浏览器的偏好加载语言(使用此变量$_SERVER['HTTP_ACCEPT_LANGUAGE'])。

Google将我的网站编入索引时出现以下错误:

Notice: Undefined index: HTTP_ACCEPT_LANGUAGE in /home/aet/web_framework/locale.php on line 20 ...

这是班级:

require_once("aet.php");

abstract class locale {

    public static function instantiate($user = false) {
        $locale = NULL;

        if ($user != false) {
            $locale = $user->getLocale();
            $locale = locale::getLocale($locale);
        }

        if ($locale == NULL) {
            if (isset($_COOKIE["locale"])) {
                $locale = $_COOKIE['locale'];
            }
            else{
                $locale = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
            }
        }

        $loc = locale::instantiateLocale($locale);

        return $loc;
    }

    public static function instantiateLocale($locale) {

        switch ($locale) {
            case 'en':
                $locale = new loc_en();
                break;
            /*case 'fr':
                $locale = new loc_fr();
                break;
            case 'de':
                $locale = new loc_de();
                break;*/
            case 'es':
                $locale = new loc_es();
                break;
            /*case 'it':
                $locale = new loc_it();
                break;*/
            default:
                $locale = new loc_es();
                break;
        }

        return $locale;
    }

    private static function getLocale($locale) {
        $aet = new aet();
        $mysqli = $aet->getAetSql();
        $loc = NULL;

        if ($stmt = $mysqli->prepare("SELECT code FROM locales WHERE id = ? LIMIT 1")) {
            $stmt->bind_param('i', $locale);
            $stmt->execute();
            $stmt->bind_result($code);

            while ($stmt->fetch()) {
                $loc = $code;
            }
        }

        return $loc;
    }

    abstract public function getW($word, $array);

    abstract protected function getVar($word, $array);
}

当机器人为我的网站编制索引时,有没有办法加载默认语言?

1 个答案:

答案 0 :(得分:1)

此:

else {
    $locale = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}

更改为:

else if (array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
    $locale = substr((string) $_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}