How to detect Opera?

Hi, just wondering why the following does not detect opera?

<html>
 <head>
  <title>Browser Detection</title>

  <link rel="stylesheet" type="text/css" href="css\BrowserStyle.css">
 </head>
   <body>
    <?php
        echo('<h1>Browser Detection</h1>');
          if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') == TRUE)
         {
            echo('<p>You are using Internet Explorer.</p>');
          }
          else if(strpos($_SERVER['HTTP_USER_AGENT'],'Firefox') == TRUE)
          {
            echo('<p>You are using Firefox.</p>');
          }  
         else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') == TRUE)
          {
            echo('<p>You are using Opera.</p>');
          }
     ?>  
   </body>
</html>
cpu4ghzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ledaveCommented:
hi,
this is because in the user agent string of Opera, Opera comes in the first place.
try this instead:
<html>
 <head>
  <title>Browser Detection</title>

  <link rel="stylesheet" type="text/css" href="css\BrowserStyle.css">
 </head>
   <body>
    <?php
       echo('<h1>Browser Detection</h1>');
         if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') == TRUE)
        {
          echo('<p>You are using Internet Explorer.</p>');
         }
         else if(strpos($_SERVER['HTTP_USER_AGENT'],'Firefox') == TRUE)
         {
          echo('<p>You are using Firefox.</p>');
         }  
        else if(stristr($_SERVER['HTTP_USER_AGENT'], 'Opera') == TRUE)
         {
          echo('<p>You are using Opera.</p>');
         }
     ?>  
   </body>
</html>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CaveyCoUkCommented:
Also, if they have changed their browser identity settings they have changed their useragent from the default Opera one to something like

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 8.50

which will be detected as MSIE so you would need ot put the "Opera" check first.

Also, if you wanted to do a better check with more browsers (Nautilus, Safari/Konqueror etc....), you might want to have a look at something like the log plugin of e107 which has a fairly good browser detection function built into it (e107 is a GPL content management system available from www.e107.org , download it and look in e107_plugins/log/loginfo.php if you want the function).  

However, be aware that people do tamper with their User Agent (especially sysadmins who think it is clever to replace it with "Nutscrape"), so you might want to have a javascript fallback (which is harder to tamper with) and then a "Oh dear, we dont know, please select an option".
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.