Mobile Detect

MobileDetect, PHP mobile detection class

Workflow status Latest tag Monthly Downloads Total Downloads MIT License

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.

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 5x \Mobile_Detect 2.8 >=5.0,<7.0 Deprecated
3.74.x 7x Detection\MobileDetect 3.74 >=7.4,<8.0 LTS
4.8.x 7x Detection\MobileDetect 4.8 >=8.0 Current, Recommended

Installing

{
  "require": {
      "mobiledetect/mobiledetectlib": "^4.8"
  }
}
// 1. Include composer's autoloader
require __DIR__ . '/vendor/autoload.php';

use Detection\MobileDetect;
// 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

  1. Submit your User-agent by visiting πŸ“± https://demo.mobiledetect.net on your device.
  2. Submit a code patch, see CONTRIBUTING.md guide.
  3. Creating a 3rd party library. The file MobileDetect.json is a model representation of this PHP script.
    It contains all the information necessary to create a "mobile" detection script in any programming language.
    You can auto-import this periodically in your repository by checking the version property, which is updated on each release.
  4. Donate πŸ‘. I'm currently paying for hosting and spend a lot of my family time to maintain the project and planning the future releases. I would highly appreciate any money donations.

Special thanks to the community πŸ‘ for donations and the JetBrains team for the open-source licenses.

Credits

Modules, plugins, ports

Submit new module, plugin, port

JavaScript

Varnish Cache

LUA

PHP

WordPress

Drupal

Joomla

Magento

PrestaShop

Laravel

Zend Framework

Symfony

Slim Framework

ExpressionEngine

Yii Framework

CakePHP

FuelPHP

TYPO3

Other

Perl

Python

Ruby

Go

LUA

.Net

ColdFusion

Experiments πŸ’‘

About

See the history of the project.