Mobile Detect

Lightweight PHP class for detecting mobile phones and tablets from the User-Agent.

Library
Composer
v4.11.0
10,681stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
71/100Good
Development Activity56
Maintenance28
Community100
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
81/100Excellent
Architecture80
Code Quality85
Innovation72
Learning Curve88

Mobile Detect is a widely used, lightweight PHP class for server-side detection of mobile devices, including tablets. It inspects the incoming User-Agent string together with specific HTTP headers to determine whether a request comes from a phone, a tablet, or a particular vendor, operating system, or browser.

With thousands of matching rules covering vendors, models, and platforms, it lets PHP applications tailor content, switch templates, trigger mobile redirects, or gate features based on device class — all before a single line of client-side JavaScript runs. It has no heavyweight dependencies beyond a PSR-16 cache interface and is a long-standing staple of the PHP ecosystem.

What You Get

  • isMobile() and isTablet() checks for quick device-class branching
  • Hundreds of vendor, model, OS, and browser detection rules
  • Dynamic magic-method checks like isIOS() or isAndroidOS()
  • PSR-16 (SimpleCache) support to cache detection results
  • A standalone build option for use without Composer autoloading

Common Use Cases

  • Serving a mobile-optimized template or layout server-side
  • Redirecting phone visitors to a dedicated mobile site or AMP page
  • Toggling features or ad slots based on device class
  • Collecting device analytics from the User-Agent on the server

Under The Hood

Architecture - The library lives under src/ in the Detection\ namespace (PSR-4), centered on a MobileDetect class that loads large rule dictionaries (device vendors, operating systems, browsers, and utility regexes) and matches them against the User-Agent and HTTP headers. Detection results can be memoized through an injected PSR-16 cache, and magic __call handling turns rule names into isX() methods.

Tech Stack - Modern PHP (>=8.2) with a single runtime dependency on psr/simple-cache. Development uses PHPUnit, PHPStan, php-cs-fixer, and phpbench for benchmarking, with a Docker Compose setup for testing across environments.

Code Quality - The 4.x line is a substantial modernization with strict typing, PHPStan static analysis, an extensive tests/ suite, and dedicated performance benchmarking (PERFORMANCE.md, phpbench). Rule data is kept in structured JSON, keeping detection logic separate from the matching engine.

API Design - The API is approachable: instantiate MobileDetect, optionally set the User-Agent, then call isMobile()/isTablet() or vendor-specific helpers. The magic-method pattern keeps the surface small while supporting a huge matrix of checks, and PSR-16 caching integrates cleanly for high-traffic sites.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search