Skip to content

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.

License

Notifications You must be signed in to change notification settings

cklm/Mobile-Detect

 
 

Repository files navigation

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.

  • 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 table 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 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

  • 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 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

  • Varnish Mobile Detect - Drop-in varnish solution to mobile user detection based on the Mobile-Detect library. Made by willemk.
  • mobiledetect2vcl - Python script to transform the Mobile Detect JSON database into an UA-based mobile detection VCL subroutine easily integrable in any Varnish Cache configuration. Made by Carlos Abalde.

LUA

  • mobile-detect.lua is a port of Mobile-Detect to Lua for NGINX HTTP servers. Follows closely to mobile-detect.js. Supports all methods that server-side mobile-detect.js supports. Fully unit-tested and synced with Travis CI (Build Passing badge included). Made by Mark Walters.

PHP

WordPress

  • Mobile Detect for WordPress - WordPress has a built-in function (wp_is_mobile()) to detect mobile devices. There is (at least) one catch, though. It considers iPad (iPad pro, and any tablet) as a mobile. So, this 3rd party module changes the way wp_is_mobile() works with the help of Mobile Detect PHP library! Made by Pothi Kalimuthu

  • WordPress Mobile Detect - Gives you the ability to wrap that infographic in a [notdevice][/notdevice] shortcode so at the server level WordPress will decide to show that content only if the user is NOT on a phone or tablet. Made by Jesse Friedman.

  • mobble - provides mobile related conditional functions for your site. e.g. is_iphone(), is_mobile() and is_tablet(). Made by Scott Evans.

  • WordPress Responsage - A small WordPress theme plugin that allows you to make your images responsive. Made by Adrian Ciaschetti.

  • WP247 Body Classes - Add unique classes to the body tag for easy styling based on various attributes (archive, user, post, mobile) and various WordPress "is" functions. Mobile attributes include type of device, Operating System, Browser, etc. Examples: .is-mobile, .is-not-mobile, .is-tablet, .is-ios, .is-not-ios, .is-androidos, .is-chromebrowser. Made by wescleveland56.

  • Adaptive Content for WordPress provides the most intuitive set of shortcodes for including/excluding content on mobile devices, tablets desktops and other more specific device parameters. This lightweight plugin lets content writers and theme authors choose when WordPress should or shouldn’t show any give content item using shortcodes and quicktags or theme elements using functions. Made by AddFunc.

  • AddFunc Mobile Detect for WordPress redirects mobile traffic to your mobile website and, basically, gives you loads of control over your mobile redirects. Made by AddFunc.

Drupal

Joomla

  • yagendoo Joomla! Mobile Detection Plugin - Lightweight PHP plugin for Joomla! that detects a mobile browser using the Mobile Detect class. Made by yagendoo media.

  • User Agent Detector plugin - This system plugin detects the user agent of your website visitor and sets a session variable accordingly. Based on the user agent, the plugin detects if the site is running on a desktop pc, tablet or smartphone. It can also detect if the visitor is a spider bot (search engine). Session variable that is set: ualayout. Possible values: desktop, tablet, mobile, bot. Made by @ReneKreijveld.

Magento

PrestaShop

  • PrestaShop is a free, secure and open source shopping cart platform. Mobile_Detect is included in the default package since 1.5.x.

Laravel

  • Agent is a user agent class for Laravel based on Mobile Detect with some additional functionality. Made by Jens Segers.

  • Laravel Mobile Detect is a package that enables you to use device detection right in your Laravel Blade templates. (Utilises the well-known, constantly updated PHP mobile detection library.) Made by Barnabas Kecskes.

  • BrowserDetect is a browser and mobile detection package, collects and wrap together the best user-agent identifiers for Laravel. Created by Varga Zsolt.

Zend Framework

  • ZF2 Mobile-Detect is a Zend Framework 2 module that provides Mobile-Detect features (Mobile_Detect class as a service, helper for views and plugin controllers). Made by neilime.

  • ZF2 MobileDetectModule facilitates integration of a PHP MobileDetect class with some ZF2-based application. Has similar idea like the existing ZF2 Mobile-Detect module, but differs in initialization and provision routine of the actual Mobile_Detect class. Appropriate view helper and controller plugin also have different conceptions. Made by Nikola Posa.

Symfony

Slim Framework

  • Slim_Mobile_Detect implements Mobile_Detect lib for different responses write on Slim Framework App.

ExpressionEngine

Yii Framework

CakePHP

FuelPHP

  • Special Agent is a FuelPHP package which uses php-mobile-detect to determine whether a device is mobile or not. It overrides the Fuelphp Agent class its methods. Made by Robbie Bardjin.

TYPO3

  • px_mobiledetect is an extension that helps to detect visitor's mobile device class (if that’s tablet or mobile device like smartphone). Made by Alexander Tretyak.

Other

  • PageCache is a lightweight PHP library for full page cache, with built-in Mobile-Detect support. Made by Muhammed Mamedov.

  • Statamic CMS Mobile Detect is a plugin. Made by Sergei Filippov of Haiku Lab.

  • Kohana Mobile Detect is an example of implementation of Mobile_Detect class with Kohana framework. Written by Luiz Alberto S. Ribeiro.

  • MemHT is a Free PHP CMS and Blog that permit the creation and the management online of websites with few and easy steps. Has the class included in the core.

  • concrete5 is a CMS that is free and open source. The library is included in the core.

  • engine7 is PHP Open Source Framework. The Mobile_Detect class is included in the engine.

  • Zikula is a free and open-source Content Management Framework, which allows you to run impressive websites and build powerful online applications. The core uses Mobile-Detect to switch to a special Mobile theme, using jQueryMobile.

  • UserAgentInfo is a PHP class for parsing user agent strings (HTTP_USER_AGENT). Includes mobile checks, bot checks, browser types/versions and more. Based on browscap, Mobile_Detect and ua-parser. Created for high traffic websites and fast batch processing. Made by quentin389.

  • LJ Mobile Detect is a simple implementation of Mobile Detect for Craft CMS. Made by Lewis Jenkins.

  • Detect Craft is a Craft CMS wrapper for the Mobile_Detect library. Made by Mikkel Rummelhoff.

  • Grav Plugin Mobile Detect is a simple implementation of Mobile Detect for Grav CMS. Made by Dimitri Longo.

  • Mobile_Detect module for UliCMS. Made by derUli.

Perl

Python

  • pymobiledetect - Mobile detect python package. Made by Bas van Oostveen.

Ruby

  • mobile_detect.rb is a Ruby gem using the JSON data exposed by the php project and implementing a basic subset of the API (as much as can be done by the exposed data). Made by Karthik T.

Go

LUA

  • ua-lua is a small lib written in LUA providing device type detection. ua-lua is detecting mobile or tablet devices based on user-agent inside nginx daemon. Made by Frédéric Robinet.

.Net

ColdFusion

Experiments 💡

About

See the history of the project.

About

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.8%
  • Shell 0.2%