<?php 
function getBrowser($useragent) {
  // check for most popular browsers first
  // unfortunately, that's IE. We also ignore Opera and Netscape 8
  // because they sometimes send msie agent
  if (strpos($useragent, 'MSIE') !== FALSE && strpos($useragent, 'Opera') === FALSE && strpos($useragent, 'Netscape') === FALSE) {
    //deal with Blazer
    if (preg_match("/Blazer\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
      return 'Blazer ' . $matches[1];
    }
    //deal with IE
    if (preg_match("/MSIE ([0-9]{1,2}\.[0-9]{1,2})/", $useragent, $matches)) {
      return 'Internet Explorer ' . $matches[1];
    }
  }
  elseif (strpos($useragent, 'IEMobile') !== FALSE) {
    if (preg_match("/IEMobile\/([0-9]{1,2}\.[0-9]{1,2})/", $useragent, $matches)) {
      return 'Internet Explorer Mobile ' . $matches[1];
    }
  }
  elseif (strpos($useragent, 'Gecko')) {
    //deal with Gecko based
    //if firefox
    if (preg_match("/Firefox\/([0-9]{1,2}\.[0-9]{1,2}(\.[0-9]{1,2})?)/", $useragent, $matches)) {
      return 'Mozilla Firefox ' . $matches[1];
    }
    //if Netscape (based on gecko)
    if (preg_match("/Netscape\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
      return 'Netscape ' . $matches[1];
    }
    //check chrome before safari because chrome agent contains both
    if (preg_match("/Chrome\/([^\s]+)/", $useragent, $matches)) {
      return 'Google Chrome ' . $matches[1];
    }
    //if Safari (based on gecko)
    if (preg_match("/Safari\/([0-9]{2,3}(\.[0-9])?)/", $useragent, $matches)) {
      return 'Safari ' . $matches[1];
    }
    //if Galeon (based on gecko)
    if (preg_match("/Galeon\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
      return 'Galeon ' . $matches[1];
    }
    //if Konqueror (based on gecko)
    if (preg_match("/Konqueror\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
      return 'Konqueror ' . $matches[1];
    }
    // if Fennec (based on gecko)
    if (preg_match("/Fennec\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
      return 'Fennec' . $matches[1];
    }
    // if Maemo (based on gecko)
    if (preg_match("/Maemo\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
      return 'Maemo' . $matches[1];
    }
    //no specific Gecko found
    //return generic Gecko
    return 'Gecko based';
  }
  elseif (strpos($useragent, 'Opera') !== FALSE) {
    //deal with Opera
    if (preg_match("/Opera[\/ ]([0-9]{1}\.[0-9]{1}([0-9])?)/", $useragent, $matches)) {
      return 'Opera ' . $matches[1];
    }
  }
  elseif (strpos($useragent, 'Lynx') !== FALSE) {
    //deal with Lynx
    if (preg_match("/Lynx\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
      return 'Lynx ' . $matches[1];
    }
  }
  elseif (strpos($useragent, 'Netscape') !== FALSE) {
    //NN8 with IE string
    if (preg_match("/Netscape\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/", $useragent, $matches)) {
      return 'Netscape ' . $matches[1];
    }
  }
  else {
    //unrecognized, this should be less than 1% of browsers (not counting bots like google etc)!
    return 'unknown';
  }
}
?>
<?php echo getBrowser($_SERVER["HTTP_USER_AGENT"]); ?>