\Mobile_Detect
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.
- Author: Current authors: Serban Ghita <serbanghita@gmail.com> Nick Ilyin <nick.ilyin@gmail.com> Original author: Victor Stanciu <vic.stanciu@gmail.com>
- License: Code and contributions have 'MIT License'
Synopsis
class Mobile_Detect
{
- // constants
- 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.24';
- const VERSION_TYPE_STRING = 'text';
- const VERSION_TYPE_FLOAT = 'float';
- // members
- protected array $cache = ;
- protected $userAgent = NULL;
- protected array $httpHeaders = ;
- protected array $cloudfrontHeaders = ;
- protected $matchingRegex = NULL;
- protected $matchesArray = NULL;
- protected string $detectionType = self::DETECTION_TYPE_MOBILE;
- protected static array $mobileHeaders = ;
- protected static array $phoneDevices = ;
- protected static array $tabletDevices = ;
- protected static array $operatingSystems = ;
- protected static array $browsers = ;
- protected static array $utilities = ;
- protected static array $uaHttpHeaders = ;
- protected static array $properties = ;
- // methods
- public void __construct()
- public static string getScriptVersion()
- public void setHttpHeaders()
- public array getHttpHeaders()
- public string|null getHttpHeader()
- public void getMobileHeaders()
- public array getUaHttpHeaders()
- public boolean setCfHeaders()
- public array getCfHeaders()
- public string|null setUserAgent()
- public string|null getUserAgent()
- public void setDetectionType()
- public void getMatchingRegex()
- public void getMatchesArray()
- public static array getPhoneDevices()
- public static array getTabletDevices()
- public static array getUserAgents()
- public static array getBrowsers()
- public static array getUtilities()
- public static array getMobileDetectionRules()
- public array getMobileDetectionRulesExtended()
- public array getRules()
- public static array getOperatingSystems()
- public bool checkHttpHeadersForMobile()
- public mixed __call()
- protected boolean matchDetectionRulesAgainstUA()
- protected boolean matchUAAgainstKey()
- public bool isMobile()
- public bool isTablet()
- public bool|int|null is()
- public bool match()
- public static array getProperties()
- public float prepareVersionNo()
- public string|float version()
- public string mobileGrade()
Tasks
Line | Task |
---|---|
1223+ | : The httpHeaders part is not yet used. |
1255+ | : search in the HTTP headers too. |
1286+ | Remove the error supression from str_replace() call. |
Constants
Name | Value |
---|---|
DETECTION_TYPE_MOBILE | 'mobile' |
DETECTION_TYPE_EXTENDED | 'extended' |
VER | '([\w._\+]+)' |
MOBILE_GRADE_A | 'A' |
MOBILE_GRADE_B | 'B' |
MOBILE_GRADE_C | 'C' |
VERSION | '2.8.24' |
VERSION_TYPE_STRING | 'text' |
VERSION_TYPE_FLOAT | 'float' |
Members
protected
- $browsers
—
array
List of mobile User Agents. - $cache
—
array
A cache for resolved matches - $cloudfrontHeaders
—
array
CloudFront headers. E.g. CloudFront-Is-Desktop-Viewer, CloudFront-Is-Mobile-Viewer & CloudFront-Is-Tablet-Viewer. - $detectionType
—
string
The detection type, using self::DETECTION_TYPE_MOBILE or self::DETECTION_TYPE_EXTENDED. - $httpHeaders
—
array
HTTP headers in the PHP-flavor. So HTTP_USER_AGENT and SERVER_SOFTWARE. - $matchesArray
—
string
The matches extracted from the regex expression. - $matchingRegex
—
string
The matching Regex. - $mobileHeaders
—
array
HTTP headers that trigger the 'isMobile' detection to be true. - $operatingSystems
—
array
List of mobile Operating Systems. - $phoneDevices
—
array
List of mobile devices (phones). - $properties
—
array
The individual segments that could exist in a User-Agent string. VER refers to the regular expression defined in the constant self::VER. - $tabletDevices
—
array
List of tablet devices. - $uaHttpHeaders
—
array
All possible HTTP headers that represent the User-Agent string. - $userAgent
—
string
The User-Agent HTTP header is stored in here. - $utilities
—
array
Utilities.
Methods
protected
- matchDetectionRulesAgainstUA() — Find a detection rule that matches the current User-agent.
- matchUAAgainstKey() — Search for a certain key in the rules array.
public
- __construct() — Construct an instance of this class.
- __call() — Magic overloading method.
- checkHttpHeadersForMobile() — Check the HTTP headers for signs of mobile.
- getBrowsers() — Retrieve the list of known browsers. Specifically, the user agents.
- getCfHeaders() — Retrieves the cloudfront headers.
- getHttpHeader() — Retrieves a particular header. If it doesn't exist, no exception/error is caused.
- getHttpHeaders() — Retrieves the HTTP headers.
- getMatchesArray()
- getMatchingRegex()
- getMobileDetectionRules() — Method gets the mobile detection rules. This method is used for the magic methods $detect->is*().
- getMobileDetectionRulesExtended() — Method gets the mobile detection rules + utilities.
- getMobileHeaders()
- getOperatingSystems() — Retrieve the list of mobile operating systems.
- getPhoneDevices() — Retrieve the list of known phone devices.
- getProperties() — Get the properties array.
- getRules() — Retrieve the current set of rules.
- getScriptVersion() — Get the current script version.
- getTabletDevices() — Retrieve the list of known tablet devices.
- getUaHttpHeaders() — Get all possible HTTP headers that can contain the User-Agent string.
- getUserAgent() — Retrieve the User-Agent.
- getUserAgents() — Alias for getBrowsers() method.
- getUtilities() — Retrieve the list of known utilities.
- is() — This method checks for a certain property in the userAgent.
- isMobile() — Check if the device is mobile.
- isTablet() — Check if the device is a tablet.
- match() — 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.
- mobileGrade() — Retrieve the mobile grading, using self::MOBILE_GRADE_* constants.
- prepareVersionNo() — Prepare the version number.
- setCfHeaders() — Set CloudFront headers http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/header-caching.html#header-caching-web-device
- setDetectionType() — Set the detection type. Must be one of self::DETECTION_TYPE_MOBILE or self::DETECTION_TYPE_EXTENDED. Otherwise, nothing is set.
- setHttpHeaders() — Set the HTTP Headers. Must be PHP-flavored. This method will reset existing headers.
- setUserAgent() — Set the User-Agent to be used.
- version() — Check the version of the given property in the User-Agent.