Browser detect code in PHP not detecting Opera -
i found code in php documentation. seems nice , simple code, can detect google chrome, firefox, ie , safari cannot detect opera.
output in opera: browser: google chrome 45.0.2454.93 on windows reports: mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/45.0.2454.93 safari/537.36 opr/32.0.1948.69
need in modifying code can detect opera.
<?php function getbrowser() { $u_agent = $_server['http_user_agent']; $bname = 'unknown'; $platform = 'unknown'; $version= ""; //first platform? if (preg_match('/linux/i', $u_agent)) { $platform = 'linux'; } elseif (preg_match('/macintosh|mac os x/i', $u_agent)) { $platform = 'mac'; } elseif (preg_match('/windows|win32/i', $u_agent)) { $platform = 'windows'; } // next name of useragent yes seperately , reason if(preg_match('/msie/i',$u_agent) && !preg_match('/opera/i',$u_agent)) { $bname = 'internet explorer'; $ub = "msie"; } elseif(preg_match('/trident/i',$u_agent)) { // condition ie11 $bname = 'internet explorer'; $ub = "rv"; } elseif(preg_match('/firefox/i',$u_agent)) { $bname = 'mozilla firefox'; $ub = "firefox"; } elseif(preg_match('/chrome/i',$u_agent)) { $bname = 'google chrome'; $ub = "chrome"; } elseif(preg_match('/safari/i',$u_agent)) { $bname = 'apple safari'; $ub = "safari"; } elseif(preg_match('/opera/i',$u_agent)) { $bname = 'opera'; $ub = "opera"; } elseif(preg_match('/netscape/i',$u_agent)) { $bname = 'netscape'; $ub = "netscape"; } // correct version number // added "|:" $known = array('version', $ub, 'other'); $pattern = '#(?<browser>' . join('|', $known) . ')[/|: ]+(?<version>[0-9.|a-za-z.]*)#'; if (!preg_match_all($pattern, $u_agent, $matches)) { // have no matching number continue } // see how many have $i = count($matches['browser']); if ($i != 1) { //we have 2 since not using 'other' argument yet //see if version before or after name if (strripos($u_agent,"version") < strripos($u_agent,$ub)){ $version= $matches['version'][0]; } else { $version= $matches['version'][1]; } } else { $version= $matches['version'][0]; } // check if have number if ($version==null || $version=="") {$version="?";} return array( 'useragent' => $u_agent, 'name' => $bname, 'version' => $version, 'platform' => $platform, 'pattern' => $pattern ); } // try $ua=getbrowser(); $yourbrowser= "your browser: " . $ua['name'] . " " . $ua['version'] . " on " .$ua['platform'] . " reports: <br >" . $ua['useragent']; print_r($yourbrowser); ?>
it's working after changes...
i changed part....
elseif(preg_match('/opera/i',$u_agent)) { $bname = 'opera'; $ub = "opera"; }
to
elseif(preg_match('/opr/i',$u_agent)) { $bname = 'opera'; $ub = "opr"; }
Comments
Post a Comment