How To 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!

ⓘ I have included a zip file with all the example code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Mobile Detection Useful Bits & Links
Tutorial Video The End

 

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

  • 1-user-agent.php is a simple “detect mobile”.
  • 2-more.php does a more detailed check.
  • 3-lib.php uses the Mobile Detect library. Please download the Mobile Detect library from their website, or pull it using Composer with composer require mobiledetect/mobiledetectlib.
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

MOBILE DETECTION

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

 

1) READING THE USER AGENT

1-user-agent.php
<?php
// (A) CHECK IF "MOBILE" EXISTS IN USER AGENT
$isMob = is_numeric(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "mobile"));
echo $isMob
  ? "Using a mobile device"
  : "Not a 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 that the user to be on a mobile device.

 

 

2) BETTER USER AGENT CHECKS

2-more.php
<?php
// (A) MOBILE DEVICE CHECK
$isMob = is_numeric(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "mobile"));
 
// (B) TABLET CHECK
$isTab = is_numeric(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "tablet"));
 
// (C) DESKTOP?
$isDesktop = !$isMob && !$isTab;
 
// (D) MANY OTHERS...
$isWin = is_numeric(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "windows"));
$isAndroid = is_numeric(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "android"));
$isIPhone = is_numeric(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "iphone"));
$isIPad = is_numeric(strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "ipad"));
$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 devices in the world, and it will be a real pain to manually do a check on all of them. So here is kind of a “convenient alternative” should you really need an 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.

 

 

USEFUL BITS & LINKS

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

 

TUTORIAL VIDEO

 

INFOGRAPHIC CHEAT SHEET

How To Detect Mobile Devices In PHP (Click to enlarge)

 

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!

3 thoughts on “How To 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.