We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

How to detect Opera?

cpu4ghz
cpu4ghz asked
on
Medium Priority
1,632 Views
Last Modified: 2010-07-27
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>
Comment
Watch Question

Commented:
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>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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".
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.