Page 1 of 1

HTTP_USER_AGENT error

Posted: Wed Jul 02, 2014 7:56 am
by simonmlewis
[text]Undefined index: HTTP_USER_AGENT[/text]

It's warming me about this code. It is simply coming from a browser or computer that doesn't report back what browser "agent" it is??
Is there a way to avoid this error?

Code: Select all

if (($count % 4) == 0)
      {
      if (strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')!==FALSE) {
echo "<div style='clear: both;' /><br/><img src='images/divider.png' /></div>";}
      elseif (strpos($_SERVER['HTTP_USER_AGENT'],'Safari')!==FALSE) {
echo "<div style='clear: both;' /><br/><img src='images/divider.png' /></div>";}
elseif (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')!==FALSE) {
echo "<div style='clear: both;' /><br/><img src='images/divider_ie.png' /></div>";}
else { echo "<div style='clear: both;' /><br/><img src='images/divider_nogap.png' /></div>";}
}		

Re: HTTP_USER_AGENT error

Posted: Wed Jul 02, 2014 8:33 am
by Celauran
First check if the array key exists using either array_key_exists() or isset()

Re: HTTP_USER_AGENT error

Posted: Wed Jul 02, 2014 8:38 am
by simonmlewis
Maybe something like this?

Code: Select all

if (($count % 4) == 0)
      {
if (isset($_SERVER['HTTP_USER_AGENT']))
{
      if (strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')!==FALSE) 
      {
      echo "<div style='clear: both;' /><br/><img src='images/divider.png' /></div>";
      }
      elseif (strpos($_SERVER['HTTP_USER_AGENT'],'Safari')!==FALSE)
      {
     echo "<div style='clear: both;' /><br/><img src='images/divider.png' /></div>";
     }
    elseif (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')!==FALSE)
     {
    echo "<div style='clear: both;' /><br/><img src='images/divider_ie.png' /></div>";
     }
    else { echo "<div style='clear: both;' /><br/><img src='images/divider_nogap.png' /></div>";}
}
else
{
    else { echo "<div style='clear: both;' /><br/><img src='images/divider_nogap.png' /></div>";}
}

}  

Re: HTTP_USER_AGENT error

Posted: Wed Jul 02, 2014 8:48 am
by Celauran
Something like that should work fine, yes.

Re: HTTP_USER_AGENT error

Posted: Wed Jul 02, 2014 9:56 am
by simonmlewis
Great thanks. Looking at it again myself, it's blindingly obvious!!