phpweb-crawlergooglebothttp-accept-language

Google crawl error with HTTP_ACCEPT_LANGUAGE


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!


Solution

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