Source for file BrowserDetection.php
Documentation is available at BrowserDetection.php
* Browser detection class file.
* This file contains everything required to use the BrowserDetection class. Tested with PHP 5.3.29 - 7.2.4.
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any
* later version (if any).
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details at: https://www.gnu.org/licenses/lgpl-3.0.html
* @package Browser_Detection
* @last-modified February 2, 2020
* @author Alexandre Valiquette
* @copyright Copyright (c) 2020, Wolfcast
* @link https://wolfcast.com/
* The BrowserDetection class facilitates the identification of the user's environment such as Web browser, version,
* platform and device type.
* $browser = new Wolfcast\BrowserDetection();
* if ($browser->getName() == Wolfcast\BrowserDetection::BROWSER_FIREFOX &&
* $browser->compareVersions($browser->getVersion(), '5.0') >= 0) {
* echo 'You are using FireFox version 5 or greater.';
* The class is a rewrite of Chris Schuld's Browser class version 1.9 which is mostly unmaintained since August 20th,
* 2010. Chris' class was based on the original work from Gary White.
* 2020-02-02: Version 2.9.5
* + WARNING! Breaking change: complete rework of robots detection. Now robot name and version is detected in addition
* of browser name and version. Use getRobotName() and getRobotVersion() when isRobot() is true.
* + WARNING! Breaking change: due to robots detection rework the following methods signatures has changed (isRobot
* parameter removed): addCustomBrowserDetection(), checkSimpleBrowserUA(), checkBrowserUAWithVersion().
* + Added possibility to support new robots with addCustomRobotDetection().
* + Added support for the new Microsoft Edge based on Chromium.
* + Added version names for Android 10 and later (Google no longer use candy names for new versions).
* + Added macOS Catalina detection.
* + Added Windows Server 2019 detection (Windows Server 2016 can be no longer detected due to the fact that they both
* use the same version number and that the build is not included in the user agent).
* 2019-03-27: Version 2.9.3
* + Fixed Edge detection on Android.
* + Added Android Q detection.
* + Now filtering superglobals.
* 2019-02-28: Version 2.9.2
* + Fixed Opera detection.
* 2018-08-23: Version 2.9.1
* + Fixed Chrome detection under iOS.
* + Added Android Pie detection.
* + Added macOS Mojave detection.
* 2018-07-15: Version 2.9.0
* + WARNING! Breaking change: new Wolfcast namespace. Use new Wolfcast\BrowserDetection().
* + iPad, iPhone and iPod are all under iOS now.
* + Added Android Oreo detection.
* + Added macOS High Sierra detection.
* + Added UC Browser detection.
* + Improved regular expressions (even less false positives).
* + Removed AOL detection.
* + Removed the following Web browsers detection: Amaya, Galeon, NetPositive, OmniWeb, Vivaldi detection (use
* addCustomBrowserDetection()).
* + Removed the following legacy platforms detection: BeOS, OS/2, SunOS (use addCustomPlatformDetection()).
* 2016-11-28: Version 2.5.1
* + Better detection of 64-bit platforms.
* 2016-08-19: Version 2.5.0
* + Platform version and platform version name are now supported for Mac.
* + Fixed platform version name for Android.
* 2016-08-02: Version 2.4.0
* + Platform version and platform version name are now supported for Android.
* + Added support for the Samsung Internet browser.
* + Added support for the Vivaldi browser.
* + Better support for legacy Windows versions.
* 2016-02-11: Version 2.3.0
* + WARNING! Breaking change: public method getBrowser() is renamed to getName().
* + WARNING! Breaking change: changed the compareVersions() return values to be more in line with other libraries.
* + You can now get the exact platform version (name or version numbers) on which the browser is run on with
* getPlatformVersion(). Only working with Windows operating systems at the moment.
* + You can now determine if the browser is executed from a 64-bit platform with is64bitPlatform().
* + Better detection of mobile platform for Googlebot.
* 2016-01-04: Version 2.2.0
* + Added support for Microsoft Edge.
* 2014-12-30: Version 2.1.2
* + Better detection of Opera.
* 2014-07-11: Version 2.1.1
* + Better detection of mobile devices and platforms.
* 2014-06-04: Version 2.1.0
* + Added support for IE 11+.
* 2013-05-27: Version 2.0.0 which is (almost) a complete rewrite based on Chris Schuld's Browser class version 1.9 plus
* + Added support for Opera Mobile
* + Added support for the Windows Phone (formerly Windows Mobile) platform
* + Added support for BlackBerry Tablet OS and BlackBerry 10
* + Added support for the Symbian platform
* + Added support for Bingbot
* + Added support for the Yahoo! Multimedia crawler
* + Removed iPhone/iPad/iPod browsers since there are not browsers but platforms - test them with getPlatform()
* + Removed support for Shiretoko (Firefox 3.5 alpha/beta) and MSN Browser
* + Merged Nokia and Nokia S60
* + Updated some deprecated browser names
* + Many public methods are now protected
* + Documentation updated
* + Added detection of IE compatibility view - test with getIECompatibilityView()
* + Added support for all (deprecated) Netscape versions
* + Added support for Safari < 3.0
* + Better Firefox version parsing
* + Better Opera version parsing
* + Better Mozilla detection
* @package Browser_Detection
* @last-modified February 2, 2020
* @author Alexandre Valiquette, Chris Schuld, Gary White
* @copyright Copyright (c) 2020, Wolfcast
* @license https://www.gnu.org/licenses/lgpl-3.0.html
* @link https://wolfcast.com/
* @link https://wolfcast.com/open-source/browser-detection/tutorial.php
* @link https://chrisschuld.com/
* @link https://www.apptools.com/phptools/browser/
* Constant for the name of the Web browser.
const BROWSER_ANDROID = 'Android';
const BROWSER_BLACKBERRY = 'BlackBerry';
const BROWSER_CHROME = 'Chrome';
const BROWSER_EDGE = 'Edge';
const BROWSER_FIREBIRD = 'Firebird';
const BROWSER_FIREFOX = 'Firefox';
const BROWSER_ICAB = 'iCab';
const BROWSER_ICECAT = 'GNU IceCat';
const BROWSER_ICEWEASEL = 'GNU IceWeasel';
const BROWSER_IE = 'Internet Explorer';
const BROWSER_IE_MOBILE = 'Internet Explorer Mobile';
const BROWSER_KONQUEROR = 'Konqueror';
const BROWSER_LYNX = 'Lynx';
const BROWSER_MOZILLA = 'Mozilla';
const BROWSER_MSNTV = 'MSN TV';
const BROWSER_NETSCAPE = 'Netscape';
const BROWSER_NOKIA = 'Nokia Browser';
const BROWSER_OPERA = 'Opera';
const BROWSER_OPERA_MINI = 'Opera Mini';
const BROWSER_OPERA_MOBILE = 'Opera Mobile';
const BROWSER_PHOENIX = 'Phoenix';
const BROWSER_SAFARI = 'Safari';
const BROWSER_SAMSUNG = 'Samsung Internet';
const BROWSER_TABLET_OS = 'BlackBerry Tablet OS';
const BROWSER_UC = 'UC Browser';
const BROWSER_UNKNOWN = 'unknown';
* Constant for the name of the platform on which the Web browser runs.
const PLATFORM_ANDROID = 'Android';
const PLATFORM_BLACKBERRY = 'BlackBerry';
const PLATFORM_FREEBSD = 'FreeBSD';
const PLATFORM_IOS = 'iOS';
const PLATFORM_LINUX = 'Linux';
const PLATFORM_MACINTOSH = 'Macintosh';
const PLATFORM_NETBSD = 'NetBSD';
const PLATFORM_NOKIA = 'Nokia';
const PLATFORM_OPENBSD = 'OpenBSD';
const PLATFORM_OPENSOLARIS = 'OpenSolaris';
const PLATFORM_SYMBIAN = 'Symbian';
const PLATFORM_UNKNOWN = 'unknown';
const PLATFORM_VERSION_UNKNOWN = 'unknown';
const PLATFORM_WINDOWS = 'Windows';
const PLATFORM_WINDOWS_CE = 'Windows CE';
const PLATFORM_WINDOWS_PHONE = 'Windows Phone';
* Constant for the name of the robot.
const ROBOT_BINGBOT = 'Bingbot';
const ROBOT_GOOGLEBOT = 'Googlebot';
const ROBOT_MSNBOT = 'MSNBot';
const ROBOT_SLURP = 'Yahoo! Slurp';
const ROBOT_UNKNOWN = '';
const ROBOT_VERSION_UNKNOWN = '';
const ROBOT_W3CVALIDATOR = 'W3C Validator';
const ROBOT_YAHOO_MM = 'Yahoo! Multimedia';
* Version unknown constant.
const VERSION_UNKNOWN = 'unknown';
//--- MAGIC METHODS ------------------------------------------------------------------------------------------------
* BrowserDetection class constructor.
* @param string $useragent (optional) The user agent to work with. Leave empty for the current user agent
* (contained in $_SERVER['HTTP_USER_AGENT']).
* Determine how the class will react when it is treated like a string.
* @return string Returns an HTML formatted string with a summary of the browser informations.
$values[] = array('label' => 'User agent', 'value' => $this->getUserAgent());
$values[] = array('label' => 'Browser name', 'value' => $this->getName());
$values[] = array('label' => 'Browser version', 'value' => $this->getVersion());
$values[] = array('label' => 'Platform family', 'value' => $this->getPlatform());
$values[] = array('label' => 'Platform version', 'value' => $this->getPlatformVersion(true));
$values[] = array('label' => 'Platform version name', 'value' => $this->getPlatformVersion());
$values[] = array('label' => 'Platform is 64-bit', 'value' => $this->is64bitPlatform() ? 'true' : 'false');
$values[] = array('label' => 'Is mobile', 'value' => $this->isMobile() ? 'true' : 'false');
$values[] = array('label' => 'Is robot', 'value' => $this->isRobot() ? 'true' : 'false');
$values[] = array('label' => 'Robot name', 'value' => $this->isRobot() ? ($this->getRobotName() != self::ROBOT_UNKNOWN ? $this->getRobotName() : 'Unknown') : 'Not applicable');
$values[] = array('label' => 'Robot version', 'value' => $this->isRobot() ? ($this->getRobotVersion() != self::ROBOT_VERSION_UNKNOWN ? $this->getRobotVersion() : 'Unknown') : 'Not applicable');
$values[] = array('label' => 'IE is in compatibility view', 'value' => $this->isInIECompatibilityView() ? 'true' : 'false');
$values[] = array('label' => 'Is Chrome Frame', 'value' => $this->isChromeFrame() ? 'true' : 'false');
foreach ($values as $currVal) {
$result .= '<strong>' . htmlspecialchars($currVal['label'], ENT_NOQUOTES) . ':</strong> ' . $currVal['value'] . '<br />' . PHP_EOL;
//--- PUBLIC MEMBERS -----------------------------------------------------------------------------------------------
* Dynamically add support for a new Web browser.
* @param string $browserName The Web browser name (used for display).
* @param mixed $uaNameToLookFor (optional) The string (or array of strings) representing the browser name to find
* in the user agent. If omitted, $browserName will be used.
* @param boolean $isMobile (optional) Determines if the browser is from a mobile device.
* @param string $separator (optional) The separator string used to split the browser name and the version number in
* @param boolean $uaNameFindWords (optional) Determines if the browser name to find should match a word instead of
* a part of a word. For example "Bar" would not be found in "FooBar" when true but would be found in "Foo Bar".
* When set to false, the browser name can be found anywhere in the user agent string.
* @see removeCustomBrowserDetection()
* @return boolean Returns true if the custom rule has been added, false otherwise.
public function addCustomBrowserDetection($browserName, $uaNameToLookFor = '', $isMobile = false, $separator = '/', $uaNameFindWords = true)
if ($browserName == '') {
if ($uaNameToLookFor == '') {
$uaNameToLookFor = $browserName;
$this->_customBrowserDetection[$browserName] = array('uaNameToLookFor' => $uaNameToLookFor, 'isMobile' => $isMobile == true,
'separator' => $separator, 'uaNameFindWords' => $uaNameFindWords == true);
* Dynamically add support for a new platform.
* @param string $platformName The platform name (used for display).
* @param mixed $platformNameToLookFor (optional) The string (or array of strings) representing the platform name to
* find in the user agent. If omitted, $platformName will be used.
* @param boolean $isMobile (optional) Determines if the platform is from a mobile device.
* @param boolean $uaNameFindWords (optional) Determines if the platform name to find should match a word instead of
* a part of a word. For example "Bar" would not be found in "FooBar" when true but would be found in "Foo Bar".
* @see removeCustomPlatformDetection()
* @return boolean Returns true if the custom rule has been added, false otherwise.
public function addCustomPlatformDetection($platformName, $platformNameToLookFor = '', $isMobile = false, $uaNameFindWords = true)
if ($platformName == '') {
if ($platformNameToLookFor == '') {
$platformNameToLookFor = $platformName;
'isMobile' => $isMobile == true,
'uaNameFindWords' => $uaNameFindWords == true);
* Dynamically add support for a new robot.
* @param string $robotName The robot name (used for display).
* @param mixed $uaNameToLookFor (optional) The string (or array of strings) representing the robot name to find
* in the user agent. If omitted, $robotName will be used.
* @param boolean $isMobile (optional) Determines if the robot should be considered as mobile or not.
* @param string $separator (optional) The separator string used to split the robot name and the version number in
* @param boolean $uaNameFindWords (optional) Determines if the robot name to find should match a word instead of
* a part of a word. For example "Bar" would not be found in "FooBar" when true but would be found in "Foo Bar".
* When set to false, the robot name can be found anywhere in the user agent string.
* @see removeCustomRobotDetection()
* @return boolean Returns true if the custom rule has been added, false otherwise.
public function addCustomRobotDetection($robotName, $uaNameToLookFor = '', $isMobile = false, $separator = '/', $uaNameFindWords = true)
if ($uaNameToLookFor == '') {
$uaNameToLookFor = $robotName;
$this->_customRobotDetection[$robotName] = array('uaNameToLookFor' => $uaNameToLookFor, 'isMobile' => $isMobile == true,
'separator' => $separator, 'uaNameFindWords' => $uaNameFindWords == true);
* Compare two version number strings.
* @param string $sourceVer The source version number.
* @param string $compareVer The version number to compare with the source version number.
* @return int Returns -1 if $sourceVer < $compareVer, 0 if $sourceVer == $compareVer or 1 if $sourceVer >
$sourceVer = explode('.', $sourceVer);
foreach ($sourceVer as $k => $v) {
$compareVer = explode('.', $compareVer);
foreach ($compareVer as $k => $v) {
for ($i = count($compareVer); $i < count($sourceVer); $i++ ) {
for ($i = count($sourceVer); $i < count($compareVer); $i++ ) {
foreach ($sourceVer as $i => $srcVerPart) {
if ($srcVerPart > $compareVer[$i]) {
if ($srcVerPart < $compareVer[$i]) {
* Get the name and version of the browser emulated in the compatibility view mode (if any). Since Internet
* Explorer 8, IE can be put in compatibility mode to make websites that were created for older browsers, especially
* IE 6 and 7, look better in IE 8+ which renders web pages closer to the standards and thus differently from those
* @param boolean $asArray (optional) Determines if the return value must be an array (true) or a string (false).
* @return mixed If a string was requested, the function returns the name and version of the browser emulated in
* the compatibility view mode or an empty string if the browser is not in compatibility view mode. If an array was
* requested, an array with the keys 'browser' and 'version' is returned.
* Return the BrowserDetection class version.
* @return string Returns the version as a sting with the #.#.# format.
* Get the name of the browser. All of the return values are class constants. You can compare them like this:
* $myBrowserInstance->getName() == BrowserDetection::BROWSER_FIREFOX.
* @return string Returns the name of the browser or BrowserDetection::BROWSER_UNKNOWN if unknown.
* Get the name of the platform family on which the browser is run on (such as Windows, Apple, etc.). All of
* the return values are class constants. You can compare them like this:
* $myBrowserInstance->getPlatform() == BrowserDetection::PLATFORM_ANDROID.
* @return string Returns the name of the platform or BrowserDetection::PLATFORM_UNKNOWN if unknown.
* Get the platform version on which the browser is run on. It can be returned as a string number like 'NT 6.3' or
* as a name like 'Windows 8.1'. When returning version string numbers for Windows NT OS families the number is
* prefixed by 'NT ' to differentiate from older Windows 3.x & 9x release. At the moment only the Windows and
* Android operating systems are supported.
* @param boolean $returnVersionNumbers (optional) Determines if the return value must be versions numbers as a
* string (true) or the version name (false).
* @param boolean $returnServerFlavor (optional) Since some Windows NT versions have the same values, this flag
* determines if the Server flavor is returned or not. For instance Windows 8.1 and Windows Server 2012 R2 both use
* version 6.3. This parameter is only useful when testing for Windows.
* @return string Returns the version name/version numbers of the platform or the constant PLATFORM_VERSION_UNKNOWN
public function getPlatformVersion($returnVersionNumbers = false, $returnServerFlavor = false)
return self::PLATFORM_VERSION_UNKNOWN;
if ($returnVersionNumbers) {
case self::PLATFORM_WINDOWS:
case self::PLATFORM_MACINTOSH:
case self::PLATFORM_ANDROID:
default: return self::PLATFORM_VERSION_UNKNOWN;
* Get the name of the robot. All of the return values are class constants. You can compare them like this:
* $myBrowserInstance->getRobotName() == BrowserDetection::ROBOT_GOOGLEBOT.
* @return string Returns the name of the robot or BrowserDetection::ROBOT_UNKNOWN if unknown.
* Get the version of the robot.
* @return string Returns the version of the robot or BrowserDetection::ROBOT_VERSION_UNKNOWN if unknown.
* Get the user agent value used by the class to determine the browser details.
* @return string The user agent string.
* Get the version of the browser.
* @return string Returns the version of the browser or BrowserDetection::VERSION_UNKNOWN if unknown.
* Determine if the browser is executed from a 64-bit platform. Keep in mind that not all platforms/browsers report
* this and the result may not always be accurate.
* @return boolean Returns true if the browser is executed from a 64-bit platform.
* Determine if the browser runs Google Chrome Frame (it's a plug-in designed for Internet Explorer 6+ based on the
* open-source Chromium project - it's like a Chrome browser within IE).
* @return boolean Returns true if the browser is using Google Chrome Frame, false otherwise.
* Determine if the browser is in compatibility view or not. Since Internet Explorer 8, IE can be put in
* compatibility mode to make websites that were created for older browsers, especially IE 6 and 7, look better in
* IE 8+ which renders web pages closer to the standards and thus differently from those older versions of IE.
* @return boolean Returns true if the browser is in compatibility view, false otherwise.
* Determine if the browser is from a mobile device or not.
* @return boolean Returns true if the browser is from a mobile device, false otherwise.
* Determine if the browser is a robot (Googlebot, Bingbot, Yahoo! Slurp...) or not.
* @return boolean Returns true if the browser is a robot, false otherwise.
* Remove support for a previously added Web browser.
* @param string $browserName The Web browser name as used when added.
* @see addCustomBrowserDetection()
* @return boolean Returns true if the custom rule has been found and removed, false otherwise.
* Remove support for a previously added platform.
* @param string $platformName The platform name as used when added.
* @see addCustomPlatformDetection()
* @return boolean Returns true if the custom rule has been found and removed, false otherwise.
* Remove support for a previously added robot.
* @param string $robotName The robot name as used when added.
* @see addCustomRobotDetection()
* @return boolean Returns true if the custom rule has been found and removed, false otherwise.
* Set the user agent to use with the class.
* @param string $agentString (optional) The value of the user agent. If an empty string is sent (default),
* $_SERVER['HTTP_USER_AGENT'] will be used.
//https://bugs.php.net/bug.php?id=49184
$agentString = filter_input(INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
$agentString = filter_var($_SERVER['HTTP_USER_AGENT'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
if ($agentString === false || $agentString === NULL) {
//filter_input or filter_var failed
//--- PROTECTED MEMBERS --------------------------------------------------------------------------------------------
* Convert the Android version numbers to the operating system name. For instance '1.6' returns 'Donut'.
* @param string $androidVer The Android version numbers as a string.
* @return string The operating system name or the constant PLATFORM_VERSION_UNKNOWN if nothing match the version
//https://en.wikipedia.org/wiki/Android_version_history
$majorVer = strstr($androidVer, '.', true);
return self::BROWSER_ANDROID . ' ' . $majorVer;
return 'Ice Cream Sandwich';
return self::PLATFORM_VERSION_UNKNOWN; //Unknown/unnamed Android version
* Determine if the browser is the Android browser (based on the WebKit layout engine and coupled with Chrome's
* JavaScript engine) or not.
* @return boolean Returns true if the browser is the Android browser, false otherwise.
//Android don't use the standard "Android/1.0", it uses "Android 1.0;" instead
* Determine if the browser is the BlackBerry browser or not.
* @link https://web.archive.org/web/20170328000854/http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/How-to-detect-the-BlackBerry-Browser/ta-p/559862
* @return boolean Returns true if the browser is the BlackBerry browser, false otherwise.
//Version 6, 7 & 10 check (versions 8 & 9 does not exists)
if ($this->getVersion() == self::VERSION_UNKNOWN) {
//Version 4.2 to 5.0 check
if ($this->getVersion() == self::VERSION_UNKNOWN) {
* Determine if the browser is Chrome or not.
* @link https://www.google.com/chrome/
* @return boolean Returns true if the browser is Chrome, false otherwise.
* Determine if the browser is among the custom browser rules or not. Rules are checked in the order they were
* @return boolean Returns true if we found the browser we were looking for in the custom rules, false otherwise.
$uaNameToLookFor = $customBrowser['uaNameToLookFor'];
$isMobile = $customBrowser['isMobile'];
$separator = $customBrowser['separator'];
$uaNameFindWords = $customBrowser['uaNameFindWords'];
* Determine if the browser is Edge or not.
* @return boolean Returns true if the browser is Edge, false otherwise.
* Determine if the browser is Firebird or not. Firebird was the name of Firefox from version 0.6 to 0.7.1.
* @return boolean Returns true if the browser is Firebird, false otherwise.
* Determine if the browser is Firefox or not.
* @link https://www.mozilla.org/en-US/firefox/new/
* @return boolean Returns true if the browser is Firefox, false otherwise.
//Safari heavily matches with Firefox, ensure that Safari is filtered out...
* Determine if the browser is iCab or not.
* @link http://www.icab.de/
* @return boolean Returns true if the browser is iCab, false otherwise.
//Some (early) iCab versions don't use the standard "iCab/1.0", they uses "iCab 1.0;" instead
* Determine if the browser is GNU IceCat (formerly known as GNU IceWeasel) or not.
* @link https://www.gnu.org/software/gnuzilla/
* @return boolean Returns true if the browser is GNU IceCat, false otherwise.
* Determine if the browser is GNU IceWeasel (now know as GNU IceCat) or not.
* @see checkBrowserIceCat()
* @return boolean Returns true if the browser is GNU IceWeasel, false otherwise.
* Determine if the browser is Internet Explorer or not.
* @link https://www.microsoft.com/ie/
* @link https://en.wikipedia.org/wiki/Internet_Explorer_Mobile
* @return boolean Returns true if the browser is Internet Explorer, false otherwise.
//Test for Internet Explorer Mobile (formerly Pocket Internet Explorer)
//Several browsers uses IE compatibility UAs filter these browsers out (but after testing for IE Mobile)
//Test for Internet Explorer 1
if ($this->getVersion() == self::VERSION_UNKNOWN) {
//Test for Internet Explorer 2+
//Test for Internet Explorer 11+ (check the rv: string)
//Test for Internet Explorer 8, 9 & 10 (check the Trident string)
//Trident started with version 4.0 on IE 8
$verFromTrident = $this->parseInt($foundVersion[1]) + 4;
if ($verFromTrident >= 8) {
$version = $verFromTrident . '.0';
//If we have the IE version from Trident, we can check for the compatibility view mode
foreach ($foundVersions[1] as $currVer) {
//Keep the lowest MSIE version for the emulated version (in compatibility view mode)
if ($emulatedVer == '' || $this->compareVersions($emulatedVer, $currVer) == 1) {
//Set the compatibility view mode if $version != $emulatedVer
//Test for Internet Explorer 2-7 versions if needed
foreach ($foundVersions[1] as $currVer) {
//Keep the highest MSIE version
* Determine if the browser is Konqueror or not.
* @link https://www.konqueror.org/
* @return boolean Returns true if the browser is Konqueror, false otherwise.
* Determine if the browser is Lynx or not. It is the oldest web browser currently in general use and development.
* It is a text-based only Web browser.
* @link https://en.wikipedia.org/wiki/Lynx_(web_browser)
* @return boolean Returns true if the browser is Lynx, false otherwise.
* Determine if the browser is Mozilla or not.
* @return boolean Returns true if the browser is Mozilla, false otherwise.
* Determine if the browser is MSN TV (formerly WebTV) or not.
* @link https://en.wikipedia.org/wiki/MSN_TV
* @return boolean Returns true if the browser is WebTv, false otherwise.
* Determine if the browser is Netscape or not. Official support for this browser ended on March 1st, 2008.
* @link https://en.wikipedia.org/wiki/Netscape
* @return boolean Returns true if the browser is Netscape, false otherwise.
//BlackBerry & Nokia UAs can conflict with Netscape UAs
//Netscape v6 to v9 check
//Netscape v1-4 (v5 don't exists)
if (count($verParts) > 1) {
$verParts = explode(' ', $verParts[1]);
$verParts = explode('.', $verParts[0]);
$majorVer = $this->parseInt($verParts[0]);
if ($majorVer > 0 && $majorVer < 5) {
$version = implode('.', $verParts);
$version = substr($version, 0, - 4);
$version = substr($version, 0, - 4) . ' Gold'; //Doubles spaces (if any) will be normalized by setVersion()
* Determine if the browser is a Nokia browser or not.
* @link https://web.archive.org/web/20141012034159/http://www.developer.nokia.com/Community/Wiki/User-Agent_headers_for_Nokia_devices
* @return boolean Returns true if the browser is a Nokia browser, false otherwise.
if ($this->containString($this->_agent, array('Nokia5800', 'Nokia5530', 'Nokia5230'), true, false)) {
if ($this->checkSimpleBrowserUA(array('NokiaBrowser', 'BrowserNG', 'Series60', 'S60', 'S40OviBrowser'), $this->_agent, self::BROWSER_NOKIA, true)) {
* Determine if the browser is Opera or not.
* @link https://www.opera.com/
* @link https://www.opera.com/mobile/
* @link https://web.archive.org/web/20140220123653/http://my.opera.com/community/openweb/idopera/
* @return boolean Returns true if the browser is Opera, false otherwise.
if ($found && $this->getVersion() != self::VERSION_UNKNOWN) {
if (!$found || $version == '') {
* Determine if the browser is Phoenix or not. Phoenix was the name of Firefox from version 0.1 to 0.5.
* @return boolean Returns true if the browser is Phoenix, false otherwise.
* Determine what is the browser used by the user.
* @return boolean Returns true if the browser has been identified, false otherwise.
//Changing the check order can break the class detection results!
/* Major browsers and browsers that need to be detected in a special order */
$this->checkBrowserMsnTv() || /* MSN TV is based on IE so we must check for MSN TV before IE */
$this->checkBrowserOpera() || /* Opera must be checked before Firefox, Netscape and Chrome to avoid conflicts */
$this->checkBrowserEdge() || /* Edge must be checked before Firefox, Safari and Chrome to avoid conflicts */
$this->checkBrowserSamsung() || /* Samsung Internet browser must be checked before Chrome and Safari to avoid conflicts */
$this->checkBrowserUC() || /* UC Browser must be checked before Chrome and Safari to avoid conflicts */
$this->checkBrowserChrome() || /* Chrome must be checked before Netscaoe and Mozilla to avoid conflicts */
$this->checkBrowserIcab() || /* Check iCab before Netscape since iCab have Mozilla UAs */
$this->checkBrowserNetscape() || /* Must be checked before Firefox since Netscape 8-9 are based on Firefox */
$this->checkBrowserIceCat() || /* Check IceCat and IceWeasel before Firefox since they are GNU builds of Firefox */
/* Current browsers that don't need to be detected in any special order */
/* WebKit base check (after most other checks) */
/* Deprecated browsers that don't need to be detected in any special order */
/* Mozilla is such an open standard that it must be checked last */
* Determine if the browser is Safari or not.
* @link https://www.apple.com/safari/
* @link https://web.archive.org/web/20080514173941/http://developer.apple.com/internet/safari/uamatrix.html
* @link https://en.wikipedia.org/wiki/Safari_version_history#Release_history
* @return boolean Returns true if the browser is Safari, false otherwise.
//Check for current versions of Safari
if ($found && $this->getVersion() != self::VERSION_UNKNOWN) {
//Safari 1-2 didn't had a "Version" string in the UA, only a WebKit build and/or Safari build, extract version from these...
if (!$found || $version == '') {
if (!$found || $version == '') {
if (preg_match('/.*AppleWebKit[ (\/]*([a-z0-9.-]*)/i', $this->_agent, $matches)) {
* Determine if the browser is the Samsung Internet browser or not.
* @return boolean Returns true if the browser is the the Samsung Internet browser, false otherwise.
* Test the user agent for a specific browser that use a "Version" string (like Safari and Opera). The user agent
* should look like: "Version/1.0 Browser name/123.456" or "Browser name/123.456 Version/1.0".
* @param mixed $uaNameToLookFor The string (or array of strings) representing the browser name to find in the user
* @param string $userAgent The user agent string to work with.
* @param string $browserName The literal browser name. Always use a class constant!
* @param boolean $isMobile (optional) Determines if the browser is from a mobile device.
* @param boolean $findWords (optional) Determines if the needle should match a word to be found. For example "Bar"
* would not be found in "FooBar" when true but would be found in "Foo Bar". When set to false, the needle can be
* found anywhere in the haystack.
* @return boolean Returns true if we found the browser we were looking for, false otherwise.
protected function checkBrowserUAWithVersion($uaNameToLookFor, $userAgent, $browserName, $isMobile = false, $findWords = true)
$uaNameToLookFor = array($uaNameToLookFor);
foreach ($uaNameToLookFor as $currUANameToLookFor) {
if ($this->containString($userAgent, $currUANameToLookFor, true, $findWords)) {
if (count($verParts) > 1) {
$verParts = explode(' ', $verParts[1]);
* Determine if the browser is UC Browser or not.
* @return boolean Returns true if the browser is UC Browser, false otherwise.
* Determine the user's platform.
if ($this->containString($this->_agent, array('Windows Phone', 'IEMobile'))) { /* Check Windows Phone (formerly Windows Mobile) before Windows */
} else if ($this->containString($this->_agent, 'Windows CE')) { /* Check Windows CE before Windows */
} else if ($this->containString($this->_agent, array('CPU OS', 'CPU iPhone OS', 'iPhone', 'iPad', 'iPod'))) { /* Check iOS (iPad/iPod/iPhone) before Macintosh */
//Check if it's a 64-bit platform
if ($this->containString($this->_agent, array('WOW64', 'Win64', 'AMD64', 'x86_64', 'x86-64', 'ia64', 'IRIX64',
'ppc64', 'sparc64', 'x64;', 'x64_64'))) {
* Determine if the platform is among the custom platform rules or not. Rules are checked in the order they were
* @return boolean Returns true if we found the platform we were looking for in the custom rules, false otherwise.
$platformNameToLookFor = $customPlatform['platformNameToLookFor'];
$isMobile = $customPlatform['isMobile'];
$findWords = $customPlatform['uaNameFindWords'];
* Determine the user's platform version.
case self::PLATFORM_WINDOWS:
if (preg_match('/Windows NT\s*(\d+(?:\.\d+)*)/i', $this->_agent, $foundVersion)) {
$result = 'NT ' . $foundVersion[1];
//https://support.microsoft.com/en-us/kb/158238
} else if ($this->containString($this->_agent, array('Win 9x 4.90', 'Windows ME', 'WinME', 'Win ME'))) {
$result = '4.90.3000'; //Windows Me version range from 4.90.3000 to 4.90.3000A
$result = '4.10'; //Windows 98 version range from 4.10.1998 to 4.10.2222B
$result = '4.00'; //Windows 95 version range from 4.00.950 to 4.03.1214
} else if (($foundAt = stripos($this->_agent, 'Windows 3')) !== false) {
$result .= '.' . $foundVersion[0];
case self::PLATFORM_MACINTOSH:
case self::PLATFORM_ANDROID:
if (preg_match('/(?:CPU OS|iPhone OS|iOS)[\s_]*([\d_]+)/i', $this->_agent, $foundVersion)) {
if (trim($result) == '') {
$result = self::PLATFORM_VERSION_UNKNOWN;
* Determine if the robot is the Bingbot crawler or not.
* @link https://www.bing.com/webmaster/help/which-crawlers-does-bing-use-8c184ec0
* @return boolean Returns true if the robot is Bingbot, false otherwise.
* Determine if the robot is the Googlebot crawler or not.
* @return boolean Returns true if the robot is Googlebot, false otherwise.
* Determine if the robot is the MSNBot crawler or not. In October 2010 it was replaced by the Bingbot robot.
* @see checkRobotBingbot()
* @return boolean Returns true if the robot is MSNBot, false otherwise.
* Determine if it's a robot crawling the page and find it's name and version.
* Determine if the robot is among the custom robot rules or not. Rules are checked in the order they were added.
* @return boolean Returns true if we found the robot we were looking for in the custom rules, false otherwise.
$uaNameToLookFor = $customRobot['uaNameToLookFor'];
$isMobile = $customRobot['isMobile'];
$separator = $customRobot['separator'];
$uaNameFindWords = $customRobot['uaNameFindWords'];
* Determine if the robot is the Yahoo! Slurp crawler or not.
* @return boolean Returns true if the robot is Yahoo! Slurp, false otherwise.
* Determine if the robot is the W3C Validator or not.
* @link https://validator.w3.org/
* @return boolean Returns true if the robot is the W3C Validator, false otherwise.
//Since the W3C validates pages with different robots we will prefix our versions with the part validated on the page...
//W3C Link Checker (prefixed with "Link-")
//W3C CSS Validation Service (prefixed with "CSS-")
//W3C mobileOK Checker (prefixed with "mobileOK-")
//W3C Markup Validation Service (no prefix)
* Determine if the robot is the Yahoo! multimedia crawler or not.
* @return boolean Returns true if the robot is the Yahoo! multimedia crawler, false otherwise.
* Test the user agent for a specific browser where the browser name is immediately followed by the version number.
* The user agent should look like: "Browser name/1.0" or "Browser 1.0;".
* @param mixed $uaNameToLookFor The string (or array of strings) representing the browser name to find in the user
* @param string $userAgent The user agent string to work with.
* @param string $browserName The literal browser name. Always use a class constant!
* @param boolean $isMobile (optional) Determines if the browser is from a mobile device.
* @param string $separator (optional) The separator string used to split the browser name and the version number in
* @param boolean $uaNameFindWords (optional) Determines if the browser name to find should match a word instead of
* a part of a word. For example "Bar" would not be found in "FooBar" when true but would be found in "Foo Bar".
* When set to false, the browser name can be found anywhere in the user agent string.
* @return boolean Returns true if we found the browser we were looking for, false otherwise.
protected function checkSimpleBrowserUA($uaNameToLookFor, $userAgent, $browserName, $isMobile = false, $separator = '/', $uaNameFindWords = true)
if ($this->findAndGetVersion($uaNameToLookFor, $userAgent, $version, $separator, $uaNameFindWords)) {
* Test the user agent for a specific robot where the robot name is immediately followed by the version number.
* The user agent should look like: "Robot name/1.0" or "Robot 1.0;".
* @param mixed $uaNameToLookFor The string (or array of strings) representing the robot name to find in the user
* @param string $userAgent The user agent string to work with.
* @param string $robotName The literal robot name. Always use a class constant!
* @param string $separator (optional) The separator string used to split the robot name and the version number in
* @param boolean $uaNameFindWords (optional) Determines if the robot name to find should match a word instead of
* a part of a word. For example "Bar" would not be found in "FooBar" when true but would be found in "Foo Bar".
* When set to false, the robot name can be found anywhere in the user agent string.
* @return boolean Returns true if we found the robot we were looking for, false otherwise.
protected function checkSimpleRobot($uaNameToLookFor, $userAgent, $robotName, $separator = '/', $uaNameFindWords = true)
if ($this->findAndGetVersion($uaNameToLookFor, $userAgent, $version, $separator, $uaNameFindWords)) {
* Clean a version string from unwanted characters.
* @param string $version The version string to clean.
* @return string Returns the cleaned version number string.
//Clear anything that is in parentheses (and the parentheses themselves) - will clear started but unclosed ones too
//Replace with a space any character which is NOT an alphanumeric, dot (.), hyphen (-), underscore (_) or space
$cleanVer = preg_replace('/[^0-9.a-zA-Z_ -]/', ' ', $cleanVer);
//Remove trailing and leading spaces
$cleanVer = trim($cleanVer);
//Remove trailing dot (.), hyphen (-), underscore (_)
$cleanVer = substr($cleanVer, 0, - 1);
//Remove leading dot (.), hyphen (-), underscore (_) and character v
while (in_array(substr($cleanVer, 0, 1), array('.', '-', '_', 'v', 'V'))) {
$cleanVer = substr($cleanVer, 1);
//Remove double spaces if any
while (strpos($cleanVer, ' ') !== false) {
* Find if one or more substring is contained in a string.
* @param string $haystack The string to search in.
* @param mixed $needle The string to search for. Can be a string or an array of strings if multiples values are to
* @param boolean $insensitive (optional) Determines if we do a case-sensitive search (false) or a case-insensitive
* @param boolean $findWords (optional) Determines if the needle should match a word to be found. For example "Bar"
* would not be found in "FooBar" when true but would be found in "Foo Bar". When set to false, the needle can be
* found anywhere in the haystack.
* @return boolean Returns true if the needle (or one of the needles) has been found in the haystack, false
protected function containString($haystack, $needle, $insensitive = true, $findWords = true)
$needle = array($needle);
foreach ($needle as $currNeedle) {
$found = $this->wordPos($haystack, $currNeedle, $insensitive) !== false;
$found = stripos($haystack, $currNeedle) !== false;
$found = strpos($haystack, $currNeedle) !== false;
* Detect the user environment from the details in the user agent string.
$this->checkPlatform(); //Check the platform after the browser since some platforms can change the mobile value
* Test the user agent for a specific browser and extract it's version.
* @param type $uaNameToLookFor The string (or array of strings) representing the browser name to find in the user
* @param type $userAgent The user agent string to work with.
* @param type $version String buffer that will contain the version found (if any).
* @param type $separator (optional) The separator string used to split the browser name and the version number in
* @param type $uaNameFindWords (optional) Determines if the browser name to find should match a word instead of
* a part of a word. For example "Bar" would not be found in "FooBar" when true but would be found in "Foo Bar".
* When set to false, the browser name can be found anywhere in the user agent string.
* @return boolean Returns true if we found the browser we were looking for, false otherwise.
protected function findAndGetVersion($uaNameToLookFor, $userAgent, &$version, $separator = '/', $uaNameFindWords = true)
$uaNameToLookFor = array($uaNameToLookFor);
foreach ($uaNameToLookFor as $currUANameToLookFor) {
if ($this->containString($userAgent, $currUANameToLookFor, true, $uaNameFindWords)) {
//Many browsers don't use the standard "Browser/1.0" format, they uses "Browser 1.0;" instead
if (stripos($userAgent, $currUANameToLookFor . $separator) === false) {
$userAgent = str_ireplace($currUANameToLookFor . ' ', $currUANameToLookFor . $separator, $userAgent);
$verParts = explode($separator, stristr($userAgent, $currUANameToLookFor));
if (count($verParts) > 1) {
$verParts = explode(' ', $verParts[1]);
* Convert the iOS version numbers to the operating system name. For instance '2.0' returns 'iPhone OS 2.0'.
* @param string $iOSVer The iOS version numbers as a string.
* @return string The operating system name.
return 'iPhone OS ' . $iOSVer;
* Convert the macOS version numbers to the operating system name. For instance '10.7' returns 'Mac OS X Lion'.
* @param string $macVer The macOS version numbers as a string.
* @return string The operating system name or the constant PLATFORM_VERSION_UNKNOWN if nothing match the version
//https://en.wikipedia.org/wiki/OS_X#Release_history
return 'Mac OS X'; //Unspecified Mac OS X version
return 'macOS High Sierra';
return 'OS X El Capitan';
return 'OS X Mountain Lion';
return 'Mac OS X Snow Leopard';
return 'Mac OS X Leopard';
return 'Mac OS X Panther';
return 'Mac OS X Jaguar';
return 'Mac OS X Cheetah';
return self::PLATFORM_VERSION_UNKNOWN; //Unknown/unnamed Mac OS version
* Get the integer value of a string variable.
* @param string $intStr The scalar value being converted to an integer.
* @return int The integer value of $intStr on success, or 0 on failure.
* Reset all the properties of the class.
protected function reset()
$this->_version = self::VERSION_UNKNOWN;
* Convert a Safari build number to a Safari version number.
* @param string $version A string representing the version number.
* @link https://web.archive.org/web/20080514173941/http://developer.apple.com/internet/safari/uamatrix.html
* @return string Returns the Safari version string. If the version can't be determined, an empty string is
$verParts = explode('.', $version);
//We need a 3 parts version (version 2 will becomes 2.0.0)
while (count($verParts) < 3) {
foreach ($verParts as $i => $currPart) {
$verParts[$i] = $this->parseInt($currPart);
case 419: $result = '2.0.4';
case 417: $result = '2.0.3';
case 416: $result = '2.0.2';
if ($verParts[1] >= 11) {
case 73: $result = '0.9';
case 51: $result = '0.8.1';
case 48: $result = '0.8';
* Set if the browser is executed from a 64-bit platform.
* @param boolean $is64bit Value that tells if the browser is executed from a 64-bit platform.
* Set the name of the browser.
* @param string $browserName The name of the browser.
* Set the browser to be from a mobile device or not.
* @param boolean $isMobile (optional) Value that tells if the browser is on a mobile device or not.
protected function setMobile($isMobile = true)
* Set the platform on which the browser is on.
* @param string $platform The name of the platform.
* Set the platform version on which the browser is on.
* @param string $platformVer The version numbers of the platform.
* Set the browser to be a robot (crawler) or not.
* @param boolean $isRobot (optional) Value that tells if the browser is a robot or not.
protected function setRobot($isRobot = true)
* Set the name of the robot.
* @param string $robotName The name of the robot.
* Set the version of the robot.
* @param string $robotVersion The version of the robot.
* Set the version of the browser.
* @param string $version The version of the browser.
$this->_version = self::VERSION_UNKNOWN;
* Convert a WebKit build number to a Safari version number.
* @param string $version A string representing the version number.
* @link https://web.archive.org/web/20080514173941/http://developer.apple.com/internet/safari/uamatrix.html
* @return string Returns the Safari version string. If the version can't be determined, an empty string is
$verParts = explode('.', $version);
//We need a 3 parts version (version 2 will becomes 2.0.0)
while (count($verParts) < 3) {
foreach ($verParts as $i => $currPart) {
$verParts[$i] = $this->parseInt($currPart);
case 419: $result = '2.0.4';
case 417: $result = '2.0.3';
case 416: $result = '2.0.2';
if ($this->compareVersions('5.4', $verParts[1] . '.' . $verParts[2]) == - 1) {
$result = '1.2.4'; //125.5.5+
//WebKit 100 can be either Safari 1.1 (Safari build 100) or 1.1.1 (Safari build 100.1)
//for this reason, check the Safari build before the WebKit build.
case 100: $result = '1.1.1';
//WebKit 85.7 can be either Safari 1.0 (Safari build 85.5) or 1.0.2 (Safari build 85.7)
//for this reason, check the Safari build before the WebKit build.
case 73: $result = '0.9';
case 51: $result = '0.8.1';
case 48: $result = '0.8';
* Convert the Windows NT family version numbers to the operating system name. For instance '5.1' returns
* @param string $winVer The Windows NT family version numbers as a string.
* @param boolean $returnServerFlavor (optional) Since some Windows NT versions have the same values, this flag
* determines if the Server flavor is returned or not. For instance Windows 8.1 and Windows Server 2012 R2 both use
* @return string The operating system name or the constant PLATFORM_VERSION_UNKNOWN if nothing match the version
//https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions
$cleanWinVer = explode('.', $winVer);
while (count($cleanWinVer) > 2) {
$cleanWinVer = implode('.', $cleanWinVer);
//Future versions of Windows
return self::PLATFORM_WINDOWS . ' ' . $winVer;
//Current version of Windows
//(Windows Server 2019 & 2016 have the same version number. Only the build can separate the two - which is not included in the UA)
return $returnServerFlavor ? (self::PLATFORM_WINDOWS . ' Server 2019') : (self::PLATFORM_WINDOWS . ' 10');
return $returnServerFlavor ? (self::PLATFORM_WINDOWS . ' Server 2012 R2') : (self::PLATFORM_WINDOWS . ' 8.1');
return $returnServerFlavor ? (self::PLATFORM_WINDOWS . ' Server 2012') : (self::PLATFORM_WINDOWS . ' 8');
return $returnServerFlavor ? (self::PLATFORM_WINDOWS . ' Server 2008 R2') : (self::PLATFORM_WINDOWS . ' 7');
return $returnServerFlavor ? (self::PLATFORM_WINDOWS . ' Server 2008') : (self::PLATFORM_WINDOWS . ' Vista');
return $returnServerFlavor ? (self::PLATFORM_WINDOWS . ' Server 2003 / ' . self::PLATFORM_WINDOWS . ' Server 2003 R2') : (self::PLATFORM_WINDOWS . ' XP x64 Edition');
return self::PLATFORM_WINDOWS . ' XP';
return self::PLATFORM_WINDOWS . ' 2000';
return self::PLATFORM_WINDOWS . ' NT ' . $winVer;
return self::PLATFORM_VERSION_UNKNOWN; //Invalid Windows NT version
* Convert the Windows 3.x & 9x family version numbers to the operating system name. For instance '4.10.1998'
* @param string $winVer The Windows 3.x or 9x family version numbers as a string.
* @return string The operating system name or the constant PLATFORM_VERSION_UNKNOWN if nothing match the version
//https://support.microsoft.com/en-us/kb/158238
return self::PLATFORM_WINDOWS . ' Me'; //Normally range from 4.90.3000 to 4.90.3000A
return self::PLATFORM_WINDOWS . ' 98'; //Normally range from 4.10.1998 to 4.10.2222B
return self::PLATFORM_WINDOWS . ' 95'; //Normally range from 4.00.950 to 4.03.1214
return self::PLATFORM_WINDOWS . ' ' . $winVer;
return self::PLATFORM_WINDOWS . ' 3.1';
return self::PLATFORM_VERSION_UNKNOWN; //Invalid Windows version
* Find the position of the first occurrence of a word in a string.
* @param string $haystack The string to search in.
* @param string $needle The string to search for.
* @param boolean $insensitive (optional) Determines if we do a case-sensitive search (false) or a case-insensitive
* @param int $offset If specified, search will start this number of characters counted from the beginning of the
* string. If the offset is negative, the search will start this number of characters counted from the end of the
* @param string $foundString String buffer that will contain the exact matching needle found. Set to NULL when
* return value of the function is false.
* @return mixed Returns the position of the needle (int) if found, false otherwise. Warning this function may
* return Boolean false, but may also return a non-Boolean value which evaluates to false.
protected function wordPos($haystack, $needle, $insensitive = true, $offset = 0, &$foundString = NULL)
$haystack = substr($haystack, $offset);
foreach ($parts as $i => $currPart) {
$regex = '/(?<=\A|[\s\/\\.,;:_()-])' . implode('[\s\/\\.,;:_()-]', $parts) . '(?=[\s\/\\.,;:_()-]|$)/';
if (preg_match($regex, $haystack, $matches, PREG_OFFSET_CAPTURE)) {
$foundString = $matches[0][0];
return (int) $matches[0][1];
|