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!
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.