Google crawl error with HTTP_ACCEPT_LANGUAGE

10k views Asked by At

In my Codeigniter app I use $_SERVER['HTTP_ACCEPT_LANGUAGE'] to determine the users browser language to set the app language based on that, like that:

public function __construct()
{
parent::__construct();

/* set session language if not set. "hu" if browser language "hu", else "en" */
    if(!($this->session->userdata("lang")))
        {
        $browserlang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
        if ($browserlang == "hu")
            {
            $this->config->set_item("language", "hu");
            $this->session->set_userdata("lang", "hu");
            $this->lang->load("bh_hu", "hungarian");
    }
         else
            {
            $this->config->set_item("language", "en");
            $this->session->set_userdata("lang", "en");
            $this->lang->load("bh_en", "english");
            }
        }
     else
        {
        switch ($this->session->userdata("lang"))
             {
             case "hu": $this->lang->load("bh_hu", "hungarian"); break;
             case "en": $this->lang->load("bh_en", "english"); break;
             }
        }

     ....
  }

Everything's working fine, but as the google crawler does not send any HTTP_ACCEPT_LANGUAGE, it returns a php error Undefined index: HTTP_ACCEPT_LANGUAGE when crawling all my pages.

This is really nasty, because the php error even fills up my entire google search snippet.

Is there a way to just let the google bot ignore the HTTP_ACCEPT_LANGUAGE action?

Thanks in advance!

2

There are 2 answers

0
Hrvoje Golcic On BEST ANSWER
$browser_lang = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strtok(strip_tags($_SERVER['HTTP_ACCEPT_LANGUAGE']), ',') : '';
$browser_lang = substr($browser_lang, 0,2);

// Now check if you support this language and set it
if(array_key_exists($browser_lang, $this->languages /* define this array to compare */))
    return $browser_lang;
else{
    // return default lang
}
1
djfm On

Easy:

$browserlang = isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]) ? substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2) : '';

This way if the HTTP_ACCEPT_LANGUAGE header exists it is used and else $browserlang is set to the empty string ''; the isset() call will not produce a warning if what is inside is not defined.