Detect Mobile Or Desktop In PHP (Simple Examples)

Welcome to a tutorial on how to detect mobile or desktop in PHP. So you want to redirect the user to a mobile website, or maybe hide the “mobile-only” features on a desktop?

An easy way to detect a mobile or desktop device in PHP is to check if the HTTP user agent contains the word “mobile”.

  • $ua = strtolower($_SERVER["HTTP_USER_AGENT"]);
  • $isMob = is_numeric(strpos($ua, "mobile"));

That covers the quick basics, but we can further determine if it is a smartphone, tablet, or desktop. Let us walk through some examples in this guide – Read on!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

MOBILE DETECTION

All right, let us now get into the examples of how to detect mobile or desktop with PHP.

 

TUTORIAL VIDEO

 

1) READING THE USER AGENT

1-user-agent.php
<?php
// (A) USER AGENT
$ua = strtolower($_SERVER["HTTP_USER_AGENT"]);
 
// (B) MOBILE TABLE DESKTOP
$isMob = is_numeric(strpos($ua, "mobile"));
$isTab = is_numeric(strpos($ua, "tablet"));
$isDesk = !$isMob && !$isTab;
echo $isDesk
  ? "Desktop device"
  : "Mobile device" ;

For you guys who do not know what a “user-agent” is, it is simply header information that the browser sends to the server. We can find information on the user’s device and the platform in the user agent, for example – Windows, Linux, Mac, Android, Chrome, Safari, Edge, etc…

So it’s really that simple. As long as the user-agent string contains the word “mobile”, we will consider the user to be on a mobile device.

 

 

2) MORE USER AGENT CHECKS

2-more.php
<?php
// (A) USER AGENT
$ua = strtolower($_SERVER["HTTP_USER_AGENT"]);
 
// (B) DEVICE CHECK
$isIPhone = is_numeric(strpos($ua, "iphone"));
$isIPad = is_numeric(strpos($ua, "ipad"));
$isPixel = is_numeric(strpos($ua, "pixel"));
 
// (C) OS CHECK
$isWin = is_numeric(strpos($ua, "windows"));
$isMac = is_numeric(strpos($ua, "mac os"));
$isAndroid = is_numeric(strpos($ua, "android"));
$isIOS = $isIPhone || $isIPad;

Following up on the above, we can actually further “dissect” the user agent and check for certain keywords to determine what kind of device and/or platform the user is on.

 

3) MOBILE DETECT LIBRARY

3-lib.php
<?php
// (A) INCLUDE MOBILE DETECT LIBRARY
// DOWNLOAD FROM - http://mobiledetect.net/
require "mobile-detect/Mobile_Detect.php";
$detect = new Mobile_Detect;

// (B) DETECTION ENGINE
if ($detect->isMobile() || $detect->isTablet()) {
  echo "MOBILE OR TABLET DEVICE ";
  if ($detect->isiOS()) { echo "IOS"; }
  if ($detect->isAndroidOS()) { echo "ANDROID"; }
} else { echo "DESKTOP"; }

Finally, there are tons of mobile devices in the world. It will be a real pain to manually check all of them. So here is kind of a “convenient alternative” should you really need in-depth detection – Use the Mobile Detect library.

To “install”, simply download from their website, or use Composer to pull with composer require mobiledetect/mobiledetectlib. Once you have downloaded the Mobile Detect library, all that is left is to use it in our scripts.

 

 

EXTRAS

That’s all for the code of this tutorial, and here is a small section on some extras that may be useful to you.

 

USER AGENT LIMITATIONS

We can only get so much information out of the user agent. Please also take note that the detection is not going to be 100% accurate, as changes will occur over time – We literally have folding smartphones that are in between a tablet and phone, also tablets that act as laptops… So which is which? You decide.

P.S. The advanced users can also choose to disable the user agent, and this detection feature is going to fail.

 

A BETTER ALTERNATIVE – RESPONSIVE DESIGN

Sometimes, all it takes is to just adopt a responsive design and change the HTML/CSS. Yep, if the design is your only concern, then is it really worth the time and money to develop another dedicated mobile site? You decide.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

4 thoughts on “Detect Mobile Or Desktop In PHP (Simple Examples)”

    1. Read the tutorial carefully, especially the part on using user agents. Apple is the one responsible for using “desktop mode” as the default on iPads – AND wrongly identifying themselves as “MacOS” in the process.

      “PHP cannot detect iPad” is inaccurate. It should be “all programming languages” cannot properly detect iPad as long as Apple is not willing to fix their own user agent.

Leave a Comment

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