Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1608
  • Last Modified:

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>
0
cpu4ghz
Asked:
cpu4ghz
2 Solutions
 
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
 
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now