How to Detect Mobile Device in PHP

Recently while working on a client’s project, we came across a situation where we need to display different sliders on a website for desktop and mobile. Sometimes you may want to serve different content for mobile devices. In this article, I show you how to detect a mobile device in PHP.

There are certain scenarios where we hide sections of a website for mobile devices. These are the sections built for desktop and not for mobile. You probably create such sections differently for mobile users. Then using media queries you apply rules(show/hide) for different devices. The media queries work on the client side. The disadvantages of using media queries are that your sections are loaded even if you are hiding them on the devices. It just adds unnecessary load to the webpage. A better way to handle this is to apply conditions on the server side for mobile devices.

In another scenario, you are planning to build a different website for mobile users. When a user visits your URL you want to redirect them to your mobile website. These are a few examples where we want to detect mobile devices on the server side. There may be hundreds of different cases. In my case, it’s a different slider for desktop and mobile devices.

Having said that, let’s take a look at how to detect a mobile device in PHP.

Getting Started

To handle mobile detection, the library called Mobile-Detect is available on GitHub.

This library provides a very clean and easy way to find out if the user is on a mobile device. You can also check for Tablet devices. The library uses the User-Agent string available in HTTP headers to detect the device environment. If you are curious to know about HTTP headers, just print the $_SERVER which gives you information about headers, paths, and script location.


For the installation of this library, I recommend using Composer. Open the terminal in your project root directory and run the command below:

composer require mobiledetect/mobiledetectlib

If you don’t want to use Composer then an alternate way is to include this Mobile_Detect.php file in your project.

Detect Mobile Devices in PHP

If you have installed the library using the Composer then add the below statement at the top of your file.

require_once "vendor/autoload.php";

After this, create the instance of Mobile_Detect class and you are able to find out if your visitor is on a mobile device.

$detect = new Mobile_Detect;
if ( $detect->isMobile() ) {
    echo "You are on the mobile device.";
} else {
    echo "You are on desktop.";

For checking the Tablet device use the statement:

if ( $detect->isTablet() ) {
    echo "You are on the Tablet.";

That’s it! You are done. This is the easiest way to detect a mobile device in PHP. Let me know your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

1 thought on “How to Detect Mobile Device in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *