Skip to content

Guide · Open source

How to use Mobile-Detect: PHP mobile and tablet detection

Updated · written with AI from the project's own documentation and checked against our catalogue data

Mobile-Detect is a lightweight PHP class that checks a visitor's User-Agent string and certain HTTP headers to work out whether they are on a mobile device or a tablet. This guide is for PHP developers who want to serve a different layout or redirect mobile users on the server. By the end you will know what the library needs, how to get it, and where its detection can fall short.

Mobile Detectserbanghita/Mobile-Detect · ★ 10.7k · MIT

Quick start

  1. 1Confirm PHP 8.2 or newer. Check that your server runs PHP 8.2 or newer, the minimum for the current 4.x line.
  2. 2Clone the repository. Run the git clone command from the README to fetch the source code.
  3. 3Run the tests (optional). Use the PHPUnit command from the README to check the library works in your environment.
  4. 4Try the demo. Open the demo site on a phone or tablet to see how a device is classified.
  5. 5Add detection to your code. Use the library in your PHP request handling to decide which layout or redirect to serve, following the Gitbook documentation.
  6. 6Test with real devices. Check results on the devices your visitors use, since User-Agent data can be spoofed or incomplete.

What Mobile-Detect does

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.

Developers typically use the result to serve different layouts or redirect mobile users to a separate mobile page or app.

Check the PHP version before you install

The project keeps one branch per major line. The current 4.x line is marked as recommended and requires PHP 8.2 or newer. Since 4.10.0 the minimum is 8.2; earlier 4.x releases required 8.0.

The 2.x and 3.x lines are marked deprecated in the README, so new projects should use 4.x.

  • Current line (4.*): namespace Detection\MobileDetect, PHP 8.2 or newer
  • Deprecated 3.*: namespace Detection\MobileDetect, PHP 7.4 to under 8.0
  • Deprecated 2.*: namespace \Mobile_Detect, PHP 5.6 to under 7.0

Get the library into your project

The README's install instruction is to clone the repository. Composer is listed as a prerequisite, and the Packagist package name shown in the README badges is mobiledetect/mobiledetectlib, so Composer is the package-manager route the project points to.

Clone the repository with the command below. Check the README and documentation for the exact steps to load the class in your own application.

git clone https://github.com/serbanghita/Mobile-Detect

Run the test suite (optional)

If you cloned the repository and want to confirm it works in your environment, the README gives a PHPUnit command. It also writes a coverage report to the .coverage folder.

vendor/bin/phpunit -v -c tests/phpunit.xml --coverage-html .coverage

Test detection with the demo site

The project runs a demo where you can open the site on your own phone or tablet to see how it is detected. This is a quick way to check results before you rely on them in production.

Full documentation is hosted on Gitbook at docs.mobiledetect.net.

Know the limits of server-side detection

Detection relies on User-Agent strings and headers. Those can be spoofed or incomplete, so the library cannot guarantee accuracy.

If your layout only needs to adapt to screen size, CSS responsive design is usually enough and no server detection is required.

  • Do not use the result as a security control.
  • Test with the devices your visitors actually use.
  • Consider CSS responsive design for pure layout changes.

What people use it for

  • Redirecting mobile visitors to a separate mobile page or app
  • Serving a different server-rendered layout to phones and tablets
  • Separating tablet traffic from phone traffic on the server

Frequently asked questions

Is Mobile-Detect free to use?

Yes, the project is released under the MIT licence, as shown in the README badge and linked LICENSE file. The author asks for optional donations to support the project, but none are required.

What PHP version does Mobile-Detect need?

The current 4.x line requires PHP 8.2 or newer, and since 4.10.0 that is the minimum. The README marks 2.x and 3.x as deprecated.

How do I install Mobile-Detect?

The README gives a git clone command for the repository and lists Composer as a prerequisite. The Packagist package name shown in the badges is mobiledetect/mobiledetectlib.

Does Mobile-Detect detect tablets too?

Yes. The README describes it as a PHP class for detecting mobile devices, including tablets.

Is Mobile-Detect accurate?

Detection depends on User-Agent strings and HTTP headers, which can be spoofed or incomplete. The README does not promise guaranteed accuracy, so test it against your real traffic.

What are the alternatives to Mobile-Detect?

Comparable projects named in this directory include WURFL, DeviceAtlas PHP API and Detector. Compare them on the device data and accuracy you need.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Mobile-Detect is a good fit for PHP developers who need to tell mobile and tablet visitors apart on the server, especially with Composer and PHP 8.2 or newer. If you only need layouts that adapt to screen size, or you use a non-PHP stack, you do not need it, and projects needing guaranteed accuracy should evaluate alternatives such as WURFL or DeviceAtlas.

Install options & downloads