HTTP_USER_AGENT error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

HTTP_USER_AGENT error

Post 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>";}
}		
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTTP_USER_AGENT error

Post by Celauran »

First check if the array key exists using either array_key_exists() or isset()
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTTP_USER_AGENT error

Post 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>";}
}

}  
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HTTP_USER_AGENT error

Post by Celauran »

Something like that should work fine, yes.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: HTTP_USER_AGENT error

Post by simonmlewis »

Great thanks. Looking at it again myself, it's blindingly obvious!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply