1

My Useragent, when tested from Opera browser, reads:

Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.125 Safari/537.36 OPR/30.0.1835.88

Now, I had the following script to test Browser:

$browser_list = array(
            array('search_string' => 'chrome', 'bname' => 'Google Chrome', 'matchname' => 'chrome', 'ub' => 'Chrome'),
            array('search_string' => 'firefox', 'bname' => 'Mozilla Firefox', 'matchname' => 'firefox', 'ub' => 'Firefox'),
            array('search_string' => 'safari', 'bname' => 'Apple Safari', 'matchname' => 'Safari', 'ub' => 'Safari'),
            array('search_string' => 'netscape', 'bname' => 'Netscape', 'matchname' => 'Netscape', 'ub' => 'Netscape'),
            array('search_string' => 'opera', 'bname' => 'Opera', 'matchname' => 'Opera', 'ub' => 'Opera'), // Must be before IE
            array('search_string' => 'MSIE', 'bname' => 'Internet Explorer', 'matchname' => 'IE', 'ub' => 'MSIE'),
            array('search_string' => 'Mozilla', 'bname' => 'Internet Explorer', 'matchname' => 'IE', 'ub' => 'MSIE'), // Hack for IE 11 as the LAST one so you know not match the above
        );

    // Next get the name of the useragent yes seperately and for good reason
    foreach ($browser_list as $item) {
    echo $item['search_string'];
        if (stripos($userAgent, $item['search_string']) !== false) {
            $bname = $item['bname'];
            $matchname = $item['matchname'];
            $ub = $item['ub'];
            echo "breaking";
            break;
        }
    }

But, since the UserAgent contains all the browsername, I am facing the problem.

Also, why the usergent from Opera is such weird?

halfer
  • 19,824
  • 17
  • 99
  • 186
Saswat
  • 12,320
  • 16
  • 77
  • 156
  • Why overcomplicate things. [Here](https://github.com/cbschuld/Browser.php)'s a great and simple library that does exactly what you're looking for. – Andrei Jul 10 '15 at 11:23
  • @Andrew, Sorry can't use any library... thats the restriction put for this issue by my client – Saswat Jul 10 '15 at 11:28
  • Man, tough luck. Well either way, you could dig a bit into its source code see how he handles it. – Andrei Jul 10 '15 at 11:29
  • "Also, why the usergent from Opera is such weird?" — because browser sniffing is awful, people did awful things with it, and browsers attempted to compensate and bypass the awful things. Don't do browser sniffing. – Quentin Aug 07 '15 at 11:02
  • What do you need the browser name for? – awe Aug 14 '18 at 12:26

2 Answers2

1

Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.125 Safari/537.36 OPR/30.0.1835.88

You must first search for the "OPR/" string in the user agent. This is what Opera have in difference from the other browser.

If you find this string, stop the search. Other, continue. So it won't be confused with others browsers.

blue112
  • 52,634
  • 3
  • 45
  • 54
-1

New browsers are generally good at keeping up with new standards, and even Microsoft's "Edge" browser is quite up to speed.

Because of this, all the browser manufacturers except Firefox include "Chrome" and "Safari" in the useragent string to make sure sites that does browser sniffing to degrade functionality for other browsers are shown with full functionality. See this answer to a related question that has more details on this.

See this for a detailed walkthrough on browser detection

Generally, you should avoid browser detection, and rather use feature detection where possible, but this must be done client side where the special functionality is required.


Specifically about the Opera tag, it is so that it was identified as "Opera" up to version 12 when they used their own "Presto" engine, but after that, they started to use "Blink" based engine, so they had to start using a different tag to differentiate with the old Opera engine.

As a matter of fact, there are still quite a large number of users out there that still use Opera 12, not mainly because of the HTML engine, but because of additional advanced features, like a built-in email client.


In a test site I have, I use this javascript code that sniffs the useragent and extracts the browser and version info for display on the page. I include this to visualize how complex useragent sniffing really is. This code also supports browsers that you may not know about if they follow the pattern to have their unique Browser/version as the last one in the list (which seems to be a common pattern for latest browsers - except Chrome).

    var browserName  = navigator.appName;
    var fullVersion  = ''+parseFloat(navigator.appVersion); 
    var majorVersion = parseInt(navigator.appVersion,10);
    var nVer = navigator.appVersion;
    var nAgt = navigator.userAgent;
    var nameOffset,verOffset,ix;

    // In Opera (Presto, up to version 12), the true version is after "Opera" or after "Version"
    if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
     browserName = "Opera";
     fullVersion = nAgt.substring(verOffset+6);
     if ((verOffset=nAgt.indexOf("Version"))!=-1) 
       fullVersion = nAgt.substring(verOffset+8);
    }
    // In MSIE, the true version is after "MSIE" in userAgent
    else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
     browserName = "Microsoft Internet Explorer";
     fullVersion = nAgt.substring(verOffset+5);
    }
    // In MSIE 11, the string "MSIE" is not longer present in userAgent
    // Check for the engine "Trident" instead
    else if ((verOffset=nAgt.indexOf("Trident"))!=-1) {
     browserName = "Microsoft Internet Explorer";
     verOffset = nAgt.indexOf("rv:");
     fullVersion = nAgt.substring(verOffset+3);
    }

    // In Safari, the true version is after "Safari" or after "Version"
    // Nowdays, most browsers include "Safari", so you also need to make sure it does NOT contain "Chrom" (as they also include "Crome", but Safari don't.
    else if ((verOffset=nAgt.indexOf("Safari"))!=-1 && nAgt.indexOf("Chrom")<0) {
     browserName = "Safari";
     fullVersion = nAgt.substring(verOffset+7);
     if ((verOffset=nAgt.indexOf("Version"))!=-1) 
       fullVersion = nAgt.substring(verOffset+8);
    }
    // In most other browsers, "name/version" is at the end of userAgent 
    else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < 
              (verOffset=nAgt.lastIndexOf('/')) ) 
    {
     browserName = nAgt.substring(nameOffset,verOffset);
     if (browserName==="Safari") {
      // Safari is last when browser is Chrome. 
      // Safari is handled already, so this has to be Chrome!
      verOffset=nAgt.indexOf("Chrome") + 6;
      browserName = "Chrome";
     }
     fullVersion = nAgt.substring(verOffset+1);
     if (browserName.toLowerCase()==browserName.toUpperCase()) {
      browserName = navigator.appName;
     }
    }
    // trim the fullVersion string at semicolon/space if present
    if ((ix=fullVersion.indexOf(";"))!=-1)
       fullVersion=fullVersion.substring(0,ix);
    if ((ix=fullVersion.indexOf(" "))!=-1)
       fullVersion=fullVersion.substring(0,ix);

    majorVersion = parseInt(''+fullVersion,10);
    if (isNaN(majorVersion)) {
       fullVersion  = ''+parseFloat(navigator.appVersion); 
       majorVersion = parseInt(navigator.appVersion,10);
    }

    var engine = null;
    
    if (window.navigator.appName == "Microsoft Internet Explorer")
    {
      // This is an IE browser. What mode is the engine in?
      if (document.documentMode) // IE8 or later
        engine = document.documentMode;
      else // IE 5-7
      {
        engine = 5; // Assume quirks mode unless proven otherwise
        if (document.compatMode)
        {
          if (document.compatMode == "CSS1Compat")
            engine = 7; // standards mode
        }
        // There is no test for IE6 standards mode because that mode  
        // was replaced by IE7 standards mode; there is no emulation.
      }
      // the engine variable now contains the document compatibility mode.
    }

    var browserDetailsContent = ''
     +'<li><b>Browser name:              </b>' + browserName+'</li>'
     +'<li><b>Full version:              </b>' + fullVersion+'</li>'
     +'<li><b>Major version:             </b>' + majorVersion+'</li>'
     +'<li><b>navigator.appVersion:      </b>' + navigator.appVersion+'</li>'
     +'<li><b>navigator.appMinorVersion: </b>' + navigator.appMinorVersion+'</li>'
     +'<li><b>navigator.appCodeName:     </b>' + navigator.appCodeName+'</li>'
     +'<li><b>navigator.appName:         </b>' + navigator.appName+'</li>'
     +'<li><b>navigator.userAgent:       </b>' + navigator.userAgent+'</li>'
     +'<li><b>Internet Explorer engine:  </b>' + engine+'</li>'
    ;
    document.write(browserDetailsContent);
awe
  • 21,938
  • 6
  • 78
  • 91