simple browser detection on server side with php  [ 863 views ]

Goal: extract basic information from user agent string

To follow the user agent strings is like tilting at windmills. My suggestion is a simple way. just implement the minimum and forget the details. Let’s focus on the main browsers and the main operating systems. The first step will be this. Later we will see what is the must and we would react at that time. Don’t forget to check the device!

The following table contains your browser’s user agent string and the extracted data.
(you can test this code easily with an user agent switcher like this)

user agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
browser: unKnown
version: unKnown
os: unKnown
device: desktop

This solution is very simple. The code contains the main browser types and the main operating system types. For starting this is enough. As I said, later you can extend it depends on your needs.

browser::extract($_SERVER['HTTP_USER_AGENT']);

echo ("<table id='uas_sample' cellspacing=0>");
echo ("<tr><td class='tdr'>user agent:</td><td>".browser::$uas_. "</td></tr>");
echo ("<tr><td class='tdr'>browser:</td><td>".browser::$type_."</td></tr>");
echo ("<tr><td class='tdr'>version:</td><td>".browser::$version_."</td></tr>");
echo ("<tr><td class='tdr'>os:</td><td>".browser::$os_."</td></tr>");
echo ("<tr><td class='tdr'>device:</td><td>".browser::$device_."</td></tr>");
echo ("</table>");
echo ("<br><br>");

class browser{

  public static 
    $type = array(  
      array('Firefox', 'Firefox', 'ff'),
      array('IEMobile', 'IEMobile', 'IEMobile'),
      array('rv:11.0', 'Internet Explorer', 'ie'),
      array('MSIE', 'Internet Explorer', 'ie'),
      array('Chrome', 'Chrome', 'Chrome'),
      array('Opera', 'Opera', 'Opera'),
      array('NokiaBrowser', 'NokiaBrowser', 'NokiaBrowser'),
      array('Safari', 'Safari', 'Safari'),
      array('BrowserNG', 'BrowserNG', 'BrowserNG')            
     ),
    $os = array(  
      array('Windows CE', 'Windows CE', 'winCE'),
      array('Windows 95', 'Windows 95', 'win95'),
      array('Windows 98; Win 9x 4.90', 'Windows Me', 'winMe'),
      array('Windows 98', 'Windows 98', 'win98'),
      array('Win98', 'Windows 98', 'win98'),
      array('Windows NT 4.0', 'Windows NT 4.0', 'WinNT4.0'),
      array('Windows NT 5.01', 'Windows 2000, Service Pack 1 (SP1)', 'Win2000(SP1)'),
      array('Windows NT 5.0', 'Windows 2000', 'Win2000'),
      array('Windows NT 5.1', 'Windows XP', 'WinXP'),
      array('Windows NT 5.2', 'Windows 2003; Windows XP/x64', 'Win2003; WinXP(64)'),
      array('Windows NT 6.0', 'Windows Vista', 'Vista'),
      array('Windows NT 6.1', 'Windows 7', 'Win7'),
      array('Windows NT 6.2', 'Windows 8', 'Win8'),
      array('Windows NT 6.3', 'Windows 8.1', 'Win8.1'),
      array('Windows Phone', 'Windows Phone', 'WinPh'),
      array('Mac OS X', 'Macintosh', 'Mac'),
      array('Android', 'Android', 'Android'),
      array('Linux', 'Linux', 'Linux'),
      array('Windows Phone OS', 'Windows Phone OS', 'Windows Phone OS'),
      array('RIM Tablet OS', 'RIM Tablet OS', 'RIM Tablet OS'),
      array('Symbian', 'Symbian', 'Symbian'),
      array('MeeGo', 'MeeGo', 'MeeGo')
     ),  
    $device = array(
      array('Nexus', 'Google Nexus'),      
      array('GT-I', 'Samsung Galaxy'),  
      array('SM-N', 'Samsung Galaxy Note 3'),      
      array('GT-N', 'Samsung Galaxy Note II'),  
      array('SAMSUNG-SGH-', 'Samsung Galaxy Note'),    
      array('SCH-I', 'Samsung Galaxy Tab'),    
      array('Sony', 'Sony mobile'),        
      array('Xbox', 'Xbox'),    
      array('iPod', 'iPod'),
      array('iPhone', 'iPhone'),
      array('iPad', 'iPad'),
      array('PlayBook', 'BlackBerry PlayBook'),
      array('Kindle', 'Amazon Kindle'),
      array('HTC One X', 'HTC One X'),
      array('HTC Sensation', 'HTC Sensation'),
      array('Nokia', 'Nokia'),
      array('Windows Phone', 'Windows Phone'),
      array('Tablet', 'tablet'),
      array('Android', 'mobile'),
      array('Mobile', 'mobile')
     ),      
    $uas_ = null,
    $type_ = null,
    $version_ = null,
    $os_ = null,
    $device_ = null,
    $browser_name_sort = null;
    
  public static function extract($uas){
    self::$uas_ = $uas;
    self::typeOf($uas);
    self::version($uas);    
    self::os($uas);
    self::device($uas);    
  }  

  public static function typeOf($uas){
    $ret = 'unKnown';
    foreach(self::$type as $val)
      if(stripos($uas,$val[0]) > -1) {
        $ret = $val[1];
        self::$browser_name_sort = $val[2];
        break;
      }
    self::$type_ = $ret;
    return $ret;
  }
  
  public static function os($uas){
    $ret = 'unKnown';    
    foreach(self::$os as $val)
      if(stripos($uas,$val[0]) > -1) {$ret = $val[1]; break;}
    self::$os_ = $ret;
    return $ret;
  }  
  
  public static function device($uas){
    $ret = 'desktop';    
    foreach(self::$device as $val)
      if(stripos($uas,$val[0]) > -1) {$ret = $val[1]; break;}
    self::$device_ = $ret;
    return $ret;
  }    
  
  public static function version($uas){
    $ret = 'unKnown';    

    if(self::$type_ == 'Internet Explorer') {
      $ref = 'MSIE';
      $e1 = ';';
      $e2 = ' ';      
    }
    else{
      $ref = self::$type_;
      $e1 = ' ';
      $e2 = '/';        
    }
    
    $a = explode($e1, self::$uas_); 
    foreach($a as $val){
      $b = explode($e2, trim($val)); 
      if($b[0] == $ref){
        $ret = $b[1];
        break;
      }
    }  
    
    self::$version_ = $ret;
    return $ret;
  }  
}

see also: user agent strings

#sidebar a { color:#fff; } #sidebar ul ul li { color: #DEF585; } #sidebar h2 { color: #fff; } #sidebar ul p, #sidebar ul select { color: #BEDDBE; } #backfly { background: url(images/golfBallWallPaper.jpg) left bottom fixed repeat-x #65a51d; }