×

Welcome to TagMyCode

Please login or create account to add a snippet.
1
0
 
0
Language: PHP
Posted by: Jing Tai Piao
Added: Oct 23, 2012 10:28 PM
Views: 42
Tags: no tags
return an array with browser and version
  1. function getBrowserType(){
  2.  
  3.     $u_agent = $_SERVER['HTTP_USER_AGENT'];
  4.     $bname = 'Unknown';
  5.     $platform = 'Unknown';
  6.     $version= "";
  7.  
  8.     //First get the platform?
  9.     if (preg_match('/linux/i', $u_agent)) {
  10.         $platform = 'linux';
  11.     }
  12.     elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
  13.         $platform = 'mac';
  14.     }
  15.     elseif (preg_match('/windows|win32/i', $u_agent)) {
  16.         $platform = 'windows';
  17.     }
  18.  
  19.     // Next get the name of the useragent yes seperately and for good reason
  20.     if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
  21.     {
  22.         $bname = 'Internet Explorer';
  23.         $ub = "MSIE";
  24.     }
  25.     elseif(preg_match('/Firefox/i',$u_agent))
  26.     {
  27.         $bname = 'Mozilla Firefox';
  28.         $ub = "Firefox";
  29.     }
  30.     elseif(preg_match('/Chrome/i',$u_agent))
  31.     {
  32.         $bname = 'Google Chrome';
  33.         $ub = "Chrome";
  34.     }
  35.     elseif(preg_match('/Safari/i',$u_agent))
  36.     {
  37.         $bname = 'Apple Safari';
  38.         $ub = "Safari";
  39.     }
  40.     elseif(preg_match('/Opera/i',$u_agent))
  41.     {
  42.         $bname = 'Opera';
  43.         $ub = "Opera";
  44.     }
  45.     elseif(preg_match('/Netscape/i',$u_agent))
  46.     {
  47.         $bname = 'Netscape';
  48.         $ub = "Netscape";
  49.     }
  50.  
  51.     // finally get the correct version number
  52.     $known = array('Version', $ub, 'other');
  53.     $pattern = '#(?<browser>' . join('|', $known) .
  54.         ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
  55.     if (!preg_match_all($pattern, $u_agent, $matches)) {
  56.         // we have no matching number just continue
  57.     }
  58.  
  59.     // see how many we have
  60.     $i = count($matches['browser']);
  61.     if ($i != 1) {
  62.         //we will have two since we are not using 'other' argument yet
  63.         //see if version is before or after the name
  64.         if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
  65.             $version= $matches['version'][0];
  66.         }
  67.         else {
  68.             $version= $matches['version'][1];
  69.         }
  70.     }
  71.     else {
  72.         $version= $matches['version'][0];
  73.     }
  74.  
  75.     // check if we have a number
  76.     if ($version==null || $version=="") {$version="?";}
  77.  
  78.     return array(
  79.         'userAgent' => $u_agent,
  80.         'name'      => $bname,
  81.         'version'   => $version,
  82.         'platform'  => $platform,
  83.         'pattern'    => $pattern
  84.     );
  85. }