Odyssey
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | List of all members
Mobile_Detect Class Reference

Public Member Functions

 __construct (array $headers=null, $userAgent=null)
 
 setHttpHeaders ($httpHeaders=null)
 
 getHttpHeaders ()
 
 getHttpHeader ($header)
 
 getMobileHeaders ()
 
 getUaHttpHeaders ()
 
 setCfHeaders ($cfHeaders=null)
 
 getCfHeaders ()
 
 setUserAgent ($userAgent=null)
 
 getUserAgent ()
 
 setDetectionType ($type=null)
 
 getMatchingRegex ()
 
 getMatchesArray ()
 
 getMobileDetectionRulesExtended ()
 
 getRules ()
 
 checkHttpHeadersForMobile ()
 
 __call ($name, $arguments)
 
 isMobile ($userAgent=null, $httpHeaders=null)
 
 isTablet ($userAgent=null, $httpHeaders=null)
 
 is ($key, $userAgent=null, $httpHeaders=null)
 
 match ($regex, $userAgent=null)
 
 prepareVersionNo ($ver)
 
 version ($propertyName, $type=self::VERSION_TYPE_STRING)
 
 mobileGrade ()
 

Static Public Member Functions

static getScriptVersion ()
 
static getPhoneDevices ()
 
static getTabletDevices ()
 
static getUserAgents ()
 
static getBrowsers ()
 
static getUtilities ()
 
static getMobileDetectionRules ()
 
static getOperatingSystems ()
 
static getProperties ()
 

Public Attributes

const DETECTION_TYPE_MOBILE = 'mobile'
 
const DETECTION_TYPE_EXTENDED = 'extended'
 
const VER = '([\w._\+]+)'
 
const MOBILE_GRADE_A = 'A'
 
const MOBILE_GRADE_B = 'B'
 
const MOBILE_GRADE_C = 'C'
 
const VERSION = '2.8.33'
 
const VERSION_TYPE_STRING = 'text'
 
const VERSION_TYPE_FLOAT = 'float'
 

Protected Member Functions

 matchDetectionRulesAgainstUA ($userAgent=null)
 
 matchUAAgainstKey ($key)
 

Protected Attributes

 $cache = array()
 
 $userAgent = null
 
 $httpHeaders = array()
 
 $cloudfrontHeaders = array()
 
 $matchingRegex = null
 
 $matchesArray = null
 
 $detectionType = self::DETECTION_TYPE_MOBILE
 

Static Protected Attributes

static $mobileHeaders
 
static $phoneDevices
 
static $tabletDevices
 
static $operatingSystems
 
static $browsers
 
static $utilities
 
static $uaHttpHeaders
 
static $properties
 

Private Member Functions

 prepareUserAgent ($userAgent)
 

Detailed Description

Mobile Detect Library Motto: "Every business should have a mobile detection script to detect mobile readers"

Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

Homepage: http://mobiledetect.net GitHub: https://github.com/serbanghita/Mobile-Detect README: https://github.com/serbanghita/Mobile-Detect/blob/master/README.md CONTRIBUTING: https://github.com/serbanghita/Mobile-Detect/blob/master/docs/CONTRIBUTING.md KNOWN LIMITATIONS: https://github.com/serbanghita/Mobile-Detect/blob/master/docs/KNOWN_LIMITATIONS.md EXAMPLES: https://github.com/serbanghita/Mobile-Detect/wiki/Code-examples

@license https://github.com/serbanghita/Mobile-Detect/blob/master/LICENSE.txt MIT License

Author
Serban Ghita serba.nosp@m.nghi.nosp@m.ta@gm.nosp@m.ail..nosp@m.com
Nick Ilyin nick..nosp@m.ilyi.nosp@m.n@gma.nosp@m.il.c.nosp@m.om Original author: Victor Stanciu vic.s.nosp@m.tanc.nosp@m.iu@gm.nosp@m.ail..nosp@m.com
Version
2.8.33

Definition at line 23 of file Mobile_Detect.php.

Constructor & Destructor Documentation

◆ __construct()

Mobile_Detect::__construct ( array  $headers = null,
  $userAgent = null 
)

Construct an instance of this class.

Parameters
array$headersSpecify the headers as injection. Should be PHP SERVER flavored. If left empty, will use the global _SERVER['HTTP*'] vars instead.
string$userAgentInject the User-Agent header. If null, will use HTTP_USER_AGENT from the $headers array instead.

Definition at line 680 of file Mobile_Detect.php.

683  {
684  $this->setHttpHeaders($headers);
685  $this->setUserAgent($userAgent);
686  }
setHttpHeaders($httpHeaders=null)
setUserAgent($userAgent=null)

Member Function Documentation

◆ __call()

Mobile_Detect::__call (   $name,
  $arguments 
)

Magic overloading method.

@method boolean is[...]()

Parameters
string$name
array$arguments
Returns
mixed
Exceptions
BadMethodCallExceptionwhen the method doesn't exist and doesn't start with 'is'

Definition at line 1079 of file Mobile_Detect.php.

1080  {
1081  // make sure the name starts with 'is', otherwise
1082  if (substr($name, 0, 2) !== 'is') {
1083  throw new BadMethodCallException("No such method exists: $name");
1084  }
1085 
1086  $this->setDetectionType(self::DETECTION_TYPE_MOBILE);
1087 
1088  $key = substr($name, 2);
1089 
1090  return $this->matchUAAgainstKey($key);
1091  }
setDetectionType($type=null)

◆ checkHttpHeadersForMobile()

Mobile_Detect::checkHttpHeadersForMobile ( )

Check the HTTP headers for signs of mobile. This is the fastest mobile check possible; it's used inside isMobile() method.

Returns
bool

Definition at line 1047 of file Mobile_Detect.php.

1048  {
1049 
1050  foreach ($this->getMobileHeaders() as $mobileHeader => $matchType) {
1051  if (isset($this->httpHeaders[$mobileHeader])) {
1052  if (is_array($matchType['matches'])) {
1053  foreach ($matchType['matches'] as $_match) {
1054  if (strpos($this->httpHeaders[$mobileHeader], $_match) !== false) {
1055  return true;
1056  }
1057  }
1058 
1059  return false;
1060  } else {
1061  return true;
1062  }
1063  }
1064  }
1065 
1066  return false;
1067 
1068  }

◆ getBrowsers()

static Mobile_Detect::getBrowsers ( )
static

Retrieve the list of known browsers. Specifically, the user agents.

Returns
array List of browsers / user agents.

Definition at line 947 of file Mobile_Detect.php.

948  {
949  return self::$browsers;
950  }

◆ getCfHeaders()

Mobile_Detect::getCfHeaders ( )

Retrieves the cloudfront headers.

Returns
array

Definition at line 822 of file Mobile_Detect.php.

823  {
824  return $this->cloudfrontHeaders;
825  }

◆ getHttpHeader()

Mobile_Detect::getHttpHeader (   $header)

Retrieves a particular header. If it doesn't exist, no exception/error is caused. Simply null is returned.

Parameters
string$headerThe name of the header to retrieve. Can be HTTP compliant such as "User-Agent" or "X-Device-User-Agent" or can be php-esque with the all-caps, HTTP_ prefixed, underscore seperated awesomeness.
Returns
string|null The value of the header.

Definition at line 749 of file Mobile_Detect.php.

750  {
751  // are we using PHP-flavored headers?
752  if (strpos($header, '_') === false) {
753  $header = str_replace('-', '_', $header);
754  $header = strtoupper($header);
755  }
756 
757  // test the alternate, too
758  $altHeader = 'HTTP_' . $header;
759 
760  //Test both the regular and the HTTP_ prefix
761  if (isset($this->httpHeaders[$header])) {
762  return $this->httpHeaders[$header];
763  } elseif (isset($this->httpHeaders[$altHeader])) {
764  return $this->httpHeaders[$altHeader];
765  }
766 
767  return null;
768  }

◆ getHttpHeaders()

Mobile_Detect::getHttpHeaders ( )

Retrieves the HTTP headers.

Returns
array

Definition at line 734 of file Mobile_Detect.php.

735  {
736  return $this->httpHeaders;
737  }

◆ getMobileDetectionRules()

static Mobile_Detect::getMobileDetectionRules ( )
static

Method gets the mobile detection rules. This method is used for the magic methods $detect->is*().

Deprecated:
since version 2.6.9
Returns
array All the rules (but not extended).

Definition at line 969 of file Mobile_Detect.php.

970  {
971  static $rules;
972 
973  if (!$rules) {
974  $rules = array_merge(
975  self::$phoneDevices,
976  self::$tabletDevices,
977  self::$operatingSystems,
978  self::$browsers
979  );
980  }
981 
982  return $rules;
983 
984  }

◆ getMobileDetectionRulesExtended()

Mobile_Detect::getMobileDetectionRulesExtended ( )

Method gets the mobile detection rules + utilities. The reason this is separate is because utilities rules don't necessary imply mobile. This method is used inside the new $detect->is('stuff') method.

Deprecated:
since version 2.6.9
Returns
array All the rules + extended.

Definition at line 996 of file Mobile_Detect.php.

997  {
998  static $rules;
999 
1000  if (!$rules) {
1001  // Merge all rules together.
1002  $rules = array_merge(
1003  self::$phoneDevices,
1004  self::$tabletDevices,
1005  self::$operatingSystems,
1006  self::$browsers,
1007  self::$utilities
1008  );
1009  }
1010 
1011  return $rules;
1012  }

◆ getOperatingSystems()

static Mobile_Detect::getOperatingSystems ( )
static

Retrieve the list of mobile operating systems.

Returns
array The list of mobile operating systems.

Definition at line 1035 of file Mobile_Detect.php.

1036  {
1037  return self::$operatingSystems;
1038  }

◆ getPhoneDevices()

static Mobile_Detect::getPhoneDevices ( )
static

Retrieve the list of known phone devices.

Returns
array List of phone devices.

Definition at line 917 of file Mobile_Detect.php.

918  {
919  return self::$phoneDevices;
920  }

◆ getProperties()

static Mobile_Detect::getProperties ( )
static

Get the properties array.

Returns
array

Definition at line 1268 of file Mobile_Detect.php.

1269  {
1270  return self::$properties;
1271  }

◆ getRules()

Mobile_Detect::getRules ( )

Retrieve the current set of rules.

Deprecated:
since version 2.6.9
Returns
array

Definition at line 1021 of file Mobile_Detect.php.

1022  {
1023  if ($this->detectionType == self::DETECTION_TYPE_EXTENDED) {
1025  } else {
1027  }
1028  }
static getMobileDetectionRules()
getMobileDetectionRulesExtended()

◆ getScriptVersion()

static Mobile_Detect::getScriptVersion ( )
static

Get the current script version. This is useful for the demo.php file, so people can check on what version they are testing for mobile devices.

Returns
string The version number in semantic version format.

Definition at line 696 of file Mobile_Detect.php.

697  {
698  return self::VERSION;
699  }

◆ getTabletDevices()

static Mobile_Detect::getTabletDevices ( )
static

Retrieve the list of known tablet devices.

Returns
array List of tablet devices.

Definition at line 927 of file Mobile_Detect.php.

928  {
929  return self::$tabletDevices;
930  }

◆ getUaHttpHeaders()

Mobile_Detect::getUaHttpHeaders ( )

Get all possible HTTP headers that can contain the User-Agent string.

Returns
array List of HTTP headers.

Definition at line 781 of file Mobile_Detect.php.

782  {
783  return self::$uaHttpHeaders;
784  }

◆ getUserAgent()

Mobile_Detect::getUserAgent ( )

Retrieve the User-Agent.

Returns
string|null The user agent if it's set.

Definition at line 875 of file Mobile_Detect.php.

876  {
877  return $this->userAgent;
878  }

◆ getUserAgents()

static Mobile_Detect::getUserAgents ( )
static

Alias for getBrowsers() method.

Returns
array List of user agents.

Definition at line 937 of file Mobile_Detect.php.

938  {
939  return self::getBrowsers();
940  }
static getBrowsers()

◆ getUtilities()

static Mobile_Detect::getUtilities ( )
static

Retrieve the list of known utilities.

Returns
array List of utilities.

Definition at line 957 of file Mobile_Detect.php.

958  {
959  return self::$utilities;
960  }

◆ is()

Mobile_Detect::is (   $key,
  $userAgent = null,
  $httpHeaders = null 
)

This method checks for a certain property in the userAgent.

Todo:
: The httpHeaders part is not yet used.
Parameters
string$key
string$userAgentdeprecated
string$httpHeadersdeprecated
Returns
bool|int|null

Definition at line 1220 of file Mobile_Detect.php.

1221  {
1222  // Set the UA and HTTP headers only if needed (eg. batch mode).
1223  if ($httpHeaders) {
1224  $this->setHttpHeaders($httpHeaders);
1225  }
1226 
1227  if ($userAgent) {
1228  $this->setUserAgent($userAgent);
1229  }
1230 
1231  $this->setDetectionType(self::DETECTION_TYPE_EXTENDED);
1232 
1233  return $this->matchUAAgainstKey($key);
1234  }
setHttpHeaders($httpHeaders=null)
setDetectionType($type=null)
setUserAgent($userAgent=null)

◆ isMobile()

Mobile_Detect::isMobile (   $userAgent = null,
  $httpHeaders = null 
)

Check if the device is mobile. Returns true if any type of mobile device detected, including special ones

Parameters
null$userAgentdeprecated
null$httpHeadersdeprecated
Returns
bool

Definition at line 1152 of file Mobile_Detect.php.

1153  {
1154 
1155  if ($httpHeaders) {
1156  $this->setHttpHeaders($httpHeaders);
1157  }
1158 
1159  if ($userAgent) {
1160  $this->setUserAgent($userAgent);
1161  }
1162 
1163  // Check specifically for cloudfront headers if the useragent === 'Amazon CloudFront'
1164  if ($this->getUserAgent() === 'Amazon CloudFront') {
1165  $cfHeaders = $this->getCfHeaders();
1166  if(array_key_exists('HTTP_CLOUDFRONT_IS_MOBILE_VIEWER', $cfHeaders) && $cfHeaders['HTTP_CLOUDFRONT_IS_MOBILE_VIEWER'] === 'true') {
1167  return true;
1168  }
1169  }
1170 
1171  $this->setDetectionType(self::DETECTION_TYPE_MOBILE);
1172 
1173  if ($this->checkHttpHeadersForMobile()) {
1174  return true;
1175  } else {
1176  return $this->matchDetectionRulesAgainstUA();
1177  }
1178 
1179  }
matchDetectionRulesAgainstUA($userAgent=null)
setHttpHeaders($httpHeaders=null)
setDetectionType($type=null)
setUserAgent($userAgent=null)

◆ isTablet()

Mobile_Detect::isTablet (   $userAgent = null,
  $httpHeaders = null 
)

Check if the device is a tablet. Return true if any type of tablet device is detected.

Parameters
string$userAgentdeprecated
array$httpHeadersdeprecated
Returns
bool

Definition at line 1189 of file Mobile_Detect.php.

1190  {
1191  // Check specifically for cloudfront headers if the useragent === 'Amazon CloudFront'
1192  if ($this->getUserAgent() === 'Amazon CloudFront') {
1193  $cfHeaders = $this->getCfHeaders();
1194  if(array_key_exists('HTTP_CLOUDFRONT_IS_TABLET_VIEWER', $cfHeaders) && $cfHeaders['HTTP_CLOUDFRONT_IS_TABLET_VIEWER'] === 'true') {
1195  return true;
1196  }
1197  }
1198 
1199  $this->setDetectionType(self::DETECTION_TYPE_MOBILE);
1200 
1201  foreach (self::$tabletDevices as $_regex) {
1202  if ($this->match($_regex, $userAgent)) {
1203  return true;
1204  }
1205  }
1206 
1207  return false;
1208  }
match($regex, $userAgent=null)
setDetectionType($type=null)

◆ match()

Mobile_Detect::match (   $regex,
  $userAgent = null 
)

Some detection rules are relative (not standard), because of the diversity of devices, vendors and their conventions in representing the User-Agent or the HTTP headers.

This method will be used to check custom regexes against the User-Agent string.

Parameters
$regex
string$userAgent
Returns
bool
Todo:
: search in the HTTP headers too.

Definition at line 1251 of file Mobile_Detect.php.

1252  {
1253  $match = (bool) preg_match(sprintf('#%s#is', $regex), (false === empty($userAgent) ? $userAgent : $this->userAgent), $matches);
1254  // If positive match is found, store the results for debug.
1255  if ($match) {
1256  $this->matchingRegex = $regex;
1257  $this->matchesArray = $matches;
1258  }
1259 
1260  return $match;
1261  }

◆ matchDetectionRulesAgainstUA()

Mobile_Detect::matchDetectionRulesAgainstUA (   $userAgent = null)
protected

Find a detection rule that matches the current User-agent.

Parameters
null$userAgentdeprecated
Returns
boolean

Definition at line 1099 of file Mobile_Detect.php.

1100  {
1101  // Begin general search.
1102  foreach ($this->getRules() as $_regex) {
1103  if (empty($_regex)) {
1104  continue;
1105  }
1106 
1107  if ($this->match($_regex, $userAgent)) {
1108  return true;
1109  }
1110  }
1111 
1112  return false;
1113  }
match($regex, $userAgent=null)

◆ matchUAAgainstKey()

Mobile_Detect::matchUAAgainstKey (   $key)
protected

Search for a certain key in the rules array. If the key is found then try to match the corresponding regex against the User-Agent.

Parameters
string$key
Returns
boolean

Definition at line 1124 of file Mobile_Detect.php.

1125  {
1126  // Make the keys lowercase so we can match: isIphone(), isiPhone(), isiphone(), etc.
1127  $key = strtolower($key);
1128  if (false === isset($this->cache[$key])) {
1129 
1130  // change the keys to lower case
1131  $_rules = array_change_key_case($this->getRules());
1132 
1133  if (false === empty($_rules[$key])) {
1134  $this->cache[$key] = $this->match($_rules[$key]);
1135  }
1136 
1137  if (false === isset($this->cache[$key])) {
1138  $this->cache[$key] = false;
1139  }
1140  }
1141 
1142  return $this->cache[$key];
1143  }
match($regex, $userAgent=null)

◆ mobileGrade()

Mobile_Detect::mobileGrade ( )

Retrieve the mobile grading, using self::MOBILE_GRADE_* constants.

Returns
string One of the self::MOBILE_GRADE_* constants.

Definition at line 1352 of file Mobile_Detect.php.

1353  {
1354  $isMobile = $this->isMobile();
1355 
1356  if (
1357  // Apple iOS 4-7.0 – Tested on the original iPad (4.3 / 5.0), iPad 2 (4.3 / 5.1 / 6.1), iPad 3 (5.1 / 6.0), iPad Mini (6.1), iPad Retina (7.0), iPhone 3GS (4.3), iPhone 4 (4.3 / 5.1), iPhone 4S (5.1 / 6.0), iPhone 5 (6.0), and iPhone 5S (7.0)
1358  $this->is('iOS') && $this->version('iPad', self::VERSION_TYPE_FLOAT) >= 4.3 ||
1359  $this->is('iOS') && $this->version('iPhone', self::VERSION_TYPE_FLOAT) >= 4.3 ||
1360  $this->is('iOS') && $this->version('iPod', self::VERSION_TYPE_FLOAT) >= 4.3 ||
1361 
1362  // Android 2.1-2.3 - Tested on the HTC Incredible (2.2), original Droid (2.2), HTC Aria (2.1), Google Nexus S (2.3). Functional on 1.5 & 1.6 but performance may be sluggish, tested on Google G1 (1.5)
1363  // Android 3.1 (Honeycomb) - Tested on the Samsung Galaxy Tab 10.1 and Motorola XOOM
1364  // Android 4.0 (ICS) - Tested on a Galaxy Nexus. Note: transition performance can be poor on upgraded devices
1365  // Android 4.1 (Jelly Bean) - Tested on a Galaxy Nexus and Galaxy 7
1366  ( $this->version('Android', self::VERSION_TYPE_FLOAT)>2.1 && $this->is('Webkit') ) ||
1367 
1368  // Windows Phone 7.5-8 - Tested on the HTC Surround (7.5), HTC Trophy (7.5), LG-E900 (7.5), Nokia 800 (7.8), HTC Mazaa (7.8), Nokia Lumia 520 (8), Nokia Lumia 920 (8), HTC 8x (8)
1369  $this->version('Windows Phone OS', self::VERSION_TYPE_FLOAT) >= 7.5 ||
1370 
1371  // Tested on the Torch 9800 (6) and Style 9670 (6), BlackBerry® Torch 9810 (7), BlackBerry Z10 (10)
1372  $this->is('BlackBerry') && $this->version('BlackBerry', self::VERSION_TYPE_FLOAT) >= 6.0 ||
1373  // Blackberry Playbook (1.0-2.0) - Tested on PlayBook
1374  $this->match('Playbook.*Tablet') ||
1375 
1376  // Palm WebOS (1.4-3.0) - Tested on the Palm Pixi (1.4), Pre (1.4), Pre 2 (2.0), HP TouchPad (3.0)
1377  ( $this->version('webOS', self::VERSION_TYPE_FLOAT) >= 1.4 && $this->match('Palm|Pre|Pixi') ) ||
1378  // Palm WebOS 3.0 - Tested on HP TouchPad
1379  $this->match('hp.*TouchPad') ||
1380 
1381  // Firefox Mobile 18 - Tested on Android 2.3 and 4.1 devices
1382  ( $this->is('Firefox') && $this->version('Firefox', self::VERSION_TYPE_FLOAT) >= 18 ) ||
1383 
1384  // Chrome for Android - Tested on Android 4.0, 4.1 device
1385  ( $this->is('Chrome') && $this->is('AndroidOS') && $this->version('Android', self::VERSION_TYPE_FLOAT) >= 4.0 ) ||
1386 
1387  // Skyfire 4.1 - Tested on Android 2.3 device
1388  ( $this->is('Skyfire') && $this->version('Skyfire', self::VERSION_TYPE_FLOAT) >= 4.1 && $this->is('AndroidOS') && $this->version('Android', self::VERSION_TYPE_FLOAT) >= 2.3 ) ||
1389 
1390  // Opera Mobile 11.5-12: Tested on Android 2.3
1391  ( $this->is('Opera') && $this->version('Opera Mobi', self::VERSION_TYPE_FLOAT) >= 11.5 && $this->is('AndroidOS') ) ||
1392 
1393  // Meego 1.2 - Tested on Nokia 950 and N9
1394  $this->is('MeeGoOS') ||
1395 
1396  // Tizen (pre-release) - Tested on early hardware
1397  $this->is('Tizen') ||
1398 
1399  // Samsung Bada 2.0 - Tested on a Samsung Wave 3, Dolphin browser
1400  // @todo: more tests here!
1401  $this->is('Dolfin') && $this->version('Bada', self::VERSION_TYPE_FLOAT) >= 2.0 ||
1402 
1403  // UC Browser - Tested on Android 2.3 device
1404  ( ($this->is('UC Browser') || $this->is('Dolfin')) && $this->version('Android', self::VERSION_TYPE_FLOAT) >= 2.3 ) ||
1405 
1406  // Kindle 3 and Fire - Tested on the built-in WebKit browser for each
1407  ( $this->match('Kindle Fire') ||
1408  $this->is('Kindle') && $this->version('Kindle', self::VERSION_TYPE_FLOAT) >= 3.0 ) ||
1409 
1410  // Nook Color 1.4.1 - Tested on original Nook Color, not Nook Tablet
1411  $this->is('AndroidOS') && $this->is('NookTablet') ||
1412 
1413  // Chrome Desktop 16-24 - Tested on OS X 10.7 and Windows 7
1414  $this->version('Chrome', self::VERSION_TYPE_FLOAT) >= 16 && !$isMobile ||
1415 
1416  // Safari Desktop 5-6 - Tested on OS X 10.7 and Windows 7
1417  $this->version('Safari', self::VERSION_TYPE_FLOAT) >= 5.0 && !$isMobile ||
1418 
1419  // Firefox Desktop 10-18 - Tested on OS X 10.7 and Windows 7
1420  $this->version('Firefox', self::VERSION_TYPE_FLOAT) >= 10.0 && !$isMobile ||
1421 
1422  // Internet Explorer 7-9 - Tested on Windows XP, Vista and 7
1423  $this->version('IE', self::VERSION_TYPE_FLOAT) >= 7.0 && !$isMobile ||
1424 
1425  // Opera Desktop 10-12 - Tested on OS X 10.7 and Windows 7
1426  $this->version('Opera', self::VERSION_TYPE_FLOAT) >= 10 && !$isMobile
1427  ){
1428  return self::MOBILE_GRADE_A;
1429  }
1430 
1431  if (
1432  $this->is('iOS') && $this->version('iPad', self::VERSION_TYPE_FLOAT)<4.3 ||
1433  $this->is('iOS') && $this->version('iPhone', self::VERSION_TYPE_FLOAT)<4.3 ||
1434  $this->is('iOS') && $this->version('iPod', self::VERSION_TYPE_FLOAT)<4.3 ||
1435 
1436  // Blackberry 5.0: Tested on the Storm 2 9550, Bold 9770
1437  $this->is('Blackberry') && $this->version('BlackBerry', self::VERSION_TYPE_FLOAT) >= 5 && $this->version('BlackBerry', self::VERSION_TYPE_FLOAT)<6 ||
1438 
1439  //Opera Mini (5.0-6.5) - Tested on iOS 3.2/4.3 and Android 2.3
1440  ($this->version('Opera Mini', self::VERSION_TYPE_FLOAT) >= 5.0 && $this->version('Opera Mini', self::VERSION_TYPE_FLOAT) <= 7.0 &&
1441  ($this->version('Android', self::VERSION_TYPE_FLOAT) >= 2.3 || $this->is('iOS')) ) ||
1442 
1443  // Nokia Symbian^3 - Tested on Nokia N8 (Symbian^3), C7 (Symbian^3), also works on N97 (Symbian^1)
1444  $this->match('NokiaN8|NokiaC7|N97.*Series60|Symbian/3') ||
1445 
1446  // @todo: report this (tested on Nokia N71)
1447  $this->version('Opera Mobi', self::VERSION_TYPE_FLOAT) >= 11 && $this->is('SymbianOS')
1448  ){
1449  return self::MOBILE_GRADE_B;
1450  }
1451 
1452  if (
1453  // Blackberry 4.x - Tested on the Curve 8330
1454  $this->version('BlackBerry', self::VERSION_TYPE_FLOAT) <= 5.0 ||
1455  // Windows Mobile - Tested on the HTC Leo (WinMo 5.2)
1456  $this->match('MSIEMobile|Windows CE.*Mobile') || $this->version('Windows Mobile', self::VERSION_TYPE_FLOAT) <= 5.2 ||
1457 
1458  // Tested on original iPhone (3.1), iPhone 3 (3.2)
1459  $this->is('iOS') && $this->version('iPad', self::VERSION_TYPE_FLOAT) <= 3.2 ||
1460  $this->is('iOS') && $this->version('iPhone', self::VERSION_TYPE_FLOAT) <= 3.2 ||
1461  $this->is('iOS') && $this->version('iPod', self::VERSION_TYPE_FLOAT) <= 3.2 ||
1462 
1463  // Internet Explorer 7 and older - Tested on Windows XP
1464  $this->version('IE', self::VERSION_TYPE_FLOAT) <= 7.0 && !$isMobile
1465  ){
1466  return self::MOBILE_GRADE_C;
1467  }
1468 
1469  // All older smartphone platforms and featurephones - Any device that doesn't support media queries
1470  // will receive the basic, C grade experience.
1471  return self::MOBILE_GRADE_C;
1472  }
const MOBILE_GRADE_C
match($regex, $userAgent=null)
const MOBILE_GRADE_B
version($propertyName, $type=self::VERSION_TYPE_STRING)
isMobile($userAgent=null, $httpHeaders=null)
const MOBILE_GRADE_A
is($key, $userAgent=null, $httpHeaders=null)

◆ prepareUserAgent()

Mobile_Detect::prepareUserAgent (   $userAgent)
private
Parameters
string$userAgent
Returns
string

Definition at line 831 of file Mobile_Detect.php.

831  {
832  $userAgent = trim($userAgent);
833  $userAgent = substr($userAgent, 0, 500);
834  return $userAgent;
835  }

◆ prepareVersionNo()

Mobile_Detect::prepareVersionNo (   $ver)

Prepare the version number.

Todo:
Remove the error supression from str_replace() call.
Parameters
string$verThe string version, like "2.6.21.2152";
Returns
float

Definition at line 1282 of file Mobile_Detect.php.

1283  {
1284  $ver = str_replace(array('_', ' ', '/'), '.', $ver);
1285  $arrVer = explode('.', $ver, 2);
1286 
1287  if (isset($arrVer[1])) {
1288  $arrVer[1] = @str_replace('.', '', $arrVer[1]); // @todo: treat strings versions.
1289  }
1290 
1291  return (float) implode('.', $arrVer);
1292  }

◆ setCfHeaders()

Mobile_Detect::setCfHeaders (   $cfHeaders = null)

Set CloudFront headers http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/header-caching.html#header-caching-web-device

Parameters
array$cfHeadersList of HTTP headers
Returns
boolean If there were CloudFront headers to be set

Definition at line 795 of file Mobile_Detect.php.

795  {
796  // use global _SERVER if $cfHeaders aren't defined
797  if (!is_array($cfHeaders) || !count($cfHeaders)) {
798  $cfHeaders = $_SERVER;
799  }
800 
801  // clear existing headers
802  $this->cloudfrontHeaders = array();
803 
804  // Only save CLOUDFRONT headers. In PHP land, that means only _SERVER vars that
805  // start with cloudfront-.
806  $response = false;
807  foreach ($cfHeaders as $key => $value) {
808  if (substr(strtolower($key), 0, 16) === 'http_cloudfront_') {
809  $this->cloudfrontHeaders[strtoupper($key)] = $value;
810  $response = true;
811  }
812  }
813 
814  return $response;
815  }

◆ setDetectionType()

Mobile_Detect::setDetectionType (   $type = null)

Set the detection type. Must be one of self::DETECTION_TYPE_MOBILE or self::DETECTION_TYPE_EXTENDED. Otherwise, nothing is set.

Deprecated:
since version 2.6.9
Parameters
string$typeThe type. Must be a self::DETECTION_TYPE_* constant. The default parameter is null which will default to self::DETECTION_TYPE_MOBILE.

Definition at line 889 of file Mobile_Detect.php.

890  {
891  if ($type === null) {
893  }
894 
895  if ($type !== self::DETECTION_TYPE_MOBILE && $type !== self::DETECTION_TYPE_EXTENDED) {
896  return;
897  }
898 
899  $this->detectionType = $type;
900  }
const DETECTION_TYPE_MOBILE

◆ setHttpHeaders()

Mobile_Detect::setHttpHeaders (   $httpHeaders = null)

Set the HTTP Headers. Must be PHP-flavored. This method will reset existing headers.

Parameters
array$httpHeadersThe headers to set. If null, then using PHP's _SERVER to extract the headers. The default null is left for backwards compatibility.

Definition at line 707 of file Mobile_Detect.php.

708  {
709  // use global _SERVER if $httpHeaders aren't defined
710  if (!is_array($httpHeaders) || !count($httpHeaders)) {
711  $httpHeaders = $_SERVER;
712  }
713 
714  // clear existing headers
715  $this->httpHeaders = array();
716 
717  // Only save HTTP headers. In PHP land, that means only _SERVER vars that
718  // start with HTTP_.
719  foreach ($httpHeaders as $key => $value) {
720  if (substr($key, 0, 5) === 'HTTP_') {
721  $this->httpHeaders[$key] = $value;
722  }
723  }
724 
725  // In case we're dealing with CloudFront, we need to know.
726  $this->setCfHeaders($httpHeaders);
727  }
setCfHeaders($cfHeaders=null)

◆ setUserAgent()

Mobile_Detect::setUserAgent (   $userAgent = null)

Set the User-Agent to be used.

Parameters
string$userAgentThe user agent string to set.
Returns
string|null

Definition at line 844 of file Mobile_Detect.php.

845  {
846  // Invalidate cache due to #375
847  $this->cache = array();
848 
849  if (false === empty($userAgent)) {
850  return $this->userAgent = $this->prepareUserAgent($userAgent);
851  } else {
852  $this->userAgent = null;
853  foreach ($this->getUaHttpHeaders() as $altHeader) {
854  if (false === empty($this->httpHeaders[$altHeader])) { // @todo: should use getHttpHeader(), but it would be slow. (Serban)
855  $this->userAgent .= $this->httpHeaders[$altHeader] . " ";
856  }
857  }
858 
859  if (!empty($this->userAgent)) {
860  return $this->userAgent = $this->prepareUserAgent($this->userAgent);
861  }
862  }
863 
864  if (count($this->getCfHeaders()) > 0) {
865  return $this->userAgent = 'Amazon CloudFront';
866  }
867  return $this->userAgent = null;
868  }
prepareUserAgent($userAgent)

◆ version()

Mobile_Detect::version (   $propertyName,
  $type = self::VERSION_TYPE_STRING 
)

Check the version of the given property in the User-Agent. Will return a float number. (eg. 2_0 will return 2.0, 4.3.1 will return 4.31)

Parameters
string$propertyNameThe name of the property. See self::getProperties() array keys for all possible properties.
string$typeEither self::VERSION_TYPE_STRING to get a string value or self::VERSION_TYPE_FLOAT indicating a float value. This parameter is optional and defaults to self::VERSION_TYPE_STRING. Passing an invalid parameter will default to the this type as well.
Returns
string|float The version of the property we are trying to extract.

Definition at line 1307 of file Mobile_Detect.php.

1308  {
1309  if (empty($propertyName)) {
1310  return false;
1311  }
1312 
1313  // set the $type to the default if we don't recognize the type
1314  if ($type !== self::VERSION_TYPE_STRING && $type !== self::VERSION_TYPE_FLOAT) {
1315  $type = self::VERSION_TYPE_STRING;
1316  }
1317 
1318  $properties = self::getProperties();
1319 
1320  // Check if the property exists in the properties array.
1321  if (true === isset($properties[$propertyName])) {
1322 
1323  // Prepare the pattern to be matched.
1324  // Make sure we always deal with an array (string is converted).
1325  $properties[$propertyName] = (array) $properties[$propertyName];
1326 
1327  foreach ($properties[$propertyName] as $propertyMatchString) {
1328 
1329  $propertyPattern = str_replace('[VER]', self::VER, $propertyMatchString);
1330 
1331  // Identify and extract the version.
1332  preg_match(sprintf('#%s#is', $propertyPattern), $this->userAgent, $match);
1333 
1334  if (false === empty($match[1])) {
1335  $version = ($type == self::VERSION_TYPE_FLOAT ? $this->prepareVersionNo($match[1]) : $match[1]);
1336 
1337  return $version;
1338  }
1339 
1340  }
1341 
1342  }
1343 
1344  return false;
1345  }
const VERSION_TYPE_STRING
static getProperties()

Member Data Documentation

◆ $mobileHeaders

Mobile_Detect::$mobileHeaders
staticprotected
Initial value:
= array(
'HTTP_ACCEPT' => array('matches' => array(
'application/x-obml2d',
'application/vnd.rim.html',
'text/vnd.wap.wml',
'application/vnd.wap.xhtml+xml'
)),
'HTTP_X_WAP_PROFILE' => null,
'HTTP_X_WAP_CLIENTID' => null,
'HTTP_WAP_CONNECTION' => null,
'HTTP_PROFILE' => null,
'HTTP_X_OPERAMINI_PHONE_UA' => null,
'HTTP_X_NOKIA_GATEWAY_ID' => null,
'HTTP_X_ORANGE_ID' => null,
'HTTP_X_VODAFONE_3GPDPCONTEXT' => null,
'HTTP_X_HUAWEI_USERID' => null,
'HTTP_UA_OS' => null,
'HTTP_X_MOBILE_GATEWAY' => null,
'HTTP_X_ATT_DEVICEID' => null,
'HTTP_UA_CPU' => array('matches' => array('ARM')),
)

Definition at line 130 of file Mobile_Detect.php.

◆ $operatingSystems

Mobile_Detect::$operatingSystems
staticprotected
Initial value:
= array(
'AndroidOS' => 'Android',
'BlackBerryOS' => 'blackberry|\bBB10\b|rim tablet os',
'PalmOS' => 'PalmOS|avantgo|blazer|elaine|hiptop|palm|plucker|xiino',
'SymbianOS' => 'Symbian|SymbOS|Series60|Series40|SYB-[0-9]+|\bS60\b',
'WindowsMobileOS' => 'Windows CE.*(PPC|Smartphone|Mobile|[0-9]{3}x[0-9]{3})|Window Mobile|Windows Phone [0-9.]+|WCE;',
'WindowsPhoneOS' => 'Windows Phone 10.0|Windows Phone 8.1|Windows Phone 8.0|Windows Phone OS|XBLWP7|ZuneWP7|Windows NT 6.[23]; ARM;',
'iOS' => '\biPhone.*Mobile|\biPod|\biPad|AppleCoreMedia',
'MeeGoOS' => 'MeeGo',
'MaemoOS' => 'Maemo',
'JavaOS' => 'J2ME/|\bMIDP\b|\bCLDC\b',
'webOS' => 'webOS|hpwOS',
'badaOS' => '\bBada\b',
'BREWOS' => 'BREW',
)

Definition at line 481 of file Mobile_Detect.php.

◆ $uaHttpHeaders

Mobile_Detect::$uaHttpHeaders
staticprotected
Initial value:
= array(
'HTTP_USER_AGENT',
'HTTP_X_OPERAMINI_PHONE_UA',
'HTTP_X_DEVICE_USER_AGENT',
'HTTP_X_ORIGINAL_USER_AGENT',
'HTTP_X_SKYFIRE_PHONE',
'HTTP_X_BOLT_PHONE_UA',
'HTTP_DEVICE_STOCK_UA',
'HTTP_X_UCBROWSER_DEVICE_UA'
)

Definition at line 582 of file Mobile_Detect.php.

◆ $utilities

Mobile_Detect::$utilities
staticprotected
Initial value:
= array(
'Bot' => 'Googlebot|facebookexternalhit|AdsBot-Google|Google Keyword Suggestion|Facebot|YandexBot|YandexMobileBot|bingbot|ia_archiver|AhrefsBot|Ezooms|GSLFbot|WBSearchBot|Twitterbot|TweetmemeBot|Twikle|PaperLiBot|Wotbox|UnwindFetchor|Exabot|MJ12bot|YandexImages|TurnitinBot|Pingdom',
'MobileBot' => 'Googlebot-Mobile|AdsBot-Google-Mobile|YahooSeeker/M1A1-R2D2',
'DesktopMode' => 'WPDesktop',
'TV' => 'SonyDTV|HbbTV',
'WebKit' => '(webkit)[ /]([\w.]+)',
'Console' => '\b(Nintendo|Nintendo WiiU|Nintendo 3DS|Nintendo Switch|PLAYSTATION|Xbox)\b',
'Watch' => 'SM-V700',
)

Definition at line 561 of file Mobile_Detect.php.

◆ DETECTION_TYPE_EXTENDED

const Mobile_Detect::DETECTION_TYPE_EXTENDED = 'extended'

Extended detection type.

Deprecated:
since version 2.6.9

Definition at line 37 of file Mobile_Detect.php.

◆ DETECTION_TYPE_MOBILE

const Mobile_Detect::DETECTION_TYPE_MOBILE = 'mobile'

Mobile detection type.

Deprecated:
since version 2.6.9

Definition at line 30 of file Mobile_Detect.php.

◆ MOBILE_GRADE_A

const Mobile_Detect::MOBILE_GRADE_A = 'A'

Top-level device.

Definition at line 49 of file Mobile_Detect.php.

◆ MOBILE_GRADE_B

const Mobile_Detect::MOBILE_GRADE_B = 'B'

Mid-level device.

Definition at line 54 of file Mobile_Detect.php.

◆ MOBILE_GRADE_C

const Mobile_Detect::MOBILE_GRADE_C = 'C'

Low-level device.

Definition at line 59 of file Mobile_Detect.php.

◆ VER

const Mobile_Detect::VER = '([\w._\+]+)'

A frequently used regular expression to extract version #s.

Deprecated:
since version 2.6.9

Definition at line 44 of file Mobile_Detect.php.

◆ VERSION

const Mobile_Detect::VERSION = '2.8.33'

Stores the version number of the current release.

Definition at line 64 of file Mobile_Detect.php.

◆ VERSION_TYPE_FLOAT

const Mobile_Detect::VERSION_TYPE_FLOAT = 'float'

A type for the version() method indicating a float return value.

Definition at line 74 of file Mobile_Detect.php.

◆ VERSION_TYPE_STRING

const Mobile_Detect::VERSION_TYPE_STRING = 'text'

A type for the version() method indicating a string return value.

Definition at line 69 of file Mobile_Detect.php.


The documentation for this class was generated from the following file: