MobileDetect, PHP mobile detection class

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.

MobileDetect class is a server-side detection PHP class that can help you with your RWD strategy, it is not a replacement for CSS3 media queries or other forms of client-side feature detection. Can detect the difference between a mobile phone and a tablet by using regexes. The accuracy and relevance of the detection is kept by running tests to check for detection conflicts.

Before you install

There are three versions of MobileDetect. 4.8.x is the main version that is ALWAYS going to be updated first.

Version Tests Namespace Code PHP Version Status
2.8.x Mobile_Detect 2.8 >=5.0,<7.0 Deprecated
3.74.x DetectionMobileDetect 3.74 >=7.4,<8.0 LTS
4.8.x DetectionMobileDetect 4.8 >=8.0 Current, Recommended

Installing

Install via composer: composer require mobiledetect/mobiledetectlib Include the dependency in the composer.json file:

{ "require": { "mobiledetect/mobiledetectlib": "^4.8" } }

// 1. Include composer's autoloader require __DIR__ . '/vendor/autoload.php'; use DetectionMobileDetect; // Instantiate the class. // Here you can inject your own caching system. $detect = new MobileDetect(); // Set the user agent string from HTTP headers or manually. $detect->setUserAgent('Mozilla/5.0 (iPad; CPU OS 14_7 like Mac OS X) ...'); // Finally, check for "mobile". $deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');

Project structure

├── .github # Definitions of GitHub workflows. ├── scripts # Various utility PHP scripts for dev purposes. ├── src │ ├── Cache │ │ ├── Cache.php # PSR-16 cache array implementation. │ │ ├── CacheException.php │ │ └── CacheItem.php # Cache item that holds key, value and ttl. │ ├── Exception │ │ └── MobileDetectException.php # Generic exception. │ └── MobileDetect.php # Main library PHP code. ├── tests │ ├── Benchmark # Performance tests. │ │ └── MobileDetectBench.php │ ├── providers │ │ └── vendors # Mobile vendors (Acer, Apple, Samsung, etc.) │ │ └── ... │ ├── bootstrap.php │ ├── CacheTest.php │ ├── MobileDetectGeneralTest.php # Unit tests │ ├── MobileDetectVersionTest.php # Unit tests for $detect->version("...") │ ├── MobileDetectWithCacheTest.php # Unit tests for caching system. │ ├── UserAgentList.inc.php │ ├── UserAgentTest.php # Integration tests using User-Agents. These prevent collisions. │ ├── phpunit.xml │ └── ualist.json └── MobileDetect.json # Use this file to create a 3rd-party project.

Performance

Results are taken from a PC with 32GB RAM, i7-10700KF CPU, Win11 Pro, PHP 8.x (xdebug: yes, opcache: no)

+-------------------+--------------------------------+-----+------+-----+----------+------------------+--------+ | benchmark | subject | set | revs | its | mem_peak | mode | rstdev | +-------------------+--------------------------------+-----+------+-----+----------+------------------+--------+ | MobileDetectBench | benchIsMobileAgainstBestMatch | | 1000 | 10 | 1.866mb | 16,211.566ops/s | ±0.43% | | MobileDetectBench | benchIsMobileAgainstWorstMatch | | 1000 | 10 | 1.866mb | 2,327.531ops/s | ±0.25% | | MobileDetectBench | benchIsTabletAgainstBestMatch | | 1000 | 10 | 1.866mb | 104,689.667ops/s | ±0.42% | | MobileDetectBench | benchIsTabletAgainstWorstMatch | | 1000 | 10 | 1.866mb | 5,151.454ops/s | ±0.39% | | MobileDetectBench | benchIsIOS | | 1000 | 10 | 1.866mb | 52,449.311ops/s | ±0.38% | | MobileDetectBench | benchIsIpad | | 1000 | 10 | 1.866mb | 52,261.416ops/s | ±0.40% | | MobileDetectBench | benchIsSamsung | | 1000 | 10 | 1.866mb | 37,232.133ops/s | ±0.41% | | MobileDetectBench | benchIsSamsungTablet | | 1000 | 10 | 1.866mb | 46,380.775ops/s | ±0.49% | +-------------------+--------------------------------+-----+------+-----+----------+------------------+--------+

Contribute

Submit your User-agent by visiting

版权声明:

1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。
2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。