Simple PHP Code To Detect Mobile Device

By Faruque Ahamed Mollick

In many time you may need to detect a mobile device on a PHP base website. You may see many websites having a desktop version and also a simplified mobile version of the same website to make it simpler for mobile internet users. But to do it you must have to detect a mobile device.

Detecting a mobile device from server side is the best as it will not load unnecessary content and really make a website faster by loading only the content that you want to show on mobile device. Although, you can use CSS media query, but it will not preventing mobile devices from loading content that you want only for computers or desktop.

So the best way of detecting a mobile device from the server side. You can do it using PHP. In this tutorial, I am going to show you simple PHP code snippet which will easily detect if the visitors on your site is using a mobile device.

PHP code snippet to detect mobile device

Belo is the simple PHP code that can be used to detect a mobile device:

function isMobileDevice() {
    return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}

 How to use the above PHP code snippet?

After you place the above code you can use it like the below code:

if(isMobileDevice()){
    //Your content or code for mobile devices goes here
}
else {
    //Your content or code for desktop or computers devices
}

 Below is the complete code which you can test on mobile and desktop:

<?php
function isMobileDevice() {
    return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}

if(isMobileDevice()){
    echo "It is a mobile device";
}
else {
    echo "It is desktop or computer device";
}
?>

Now test the above code. If you test it with your mobile device then you will see the “It is a mobile device” text. And when you test it with your PC or desktop, you will see “It is desktop or computer device” text.

So how was that? Is it not so amazing and easy way of detecting a mobile device and apply if-else conditional statement?

 

4 responses to “Simple PHP Code To Detect Mobile Device”

  1. p1clem says:

    please am new to this can show me an example of how to use the code

    • Faruque Ahamed Mollick informerfrk says:

      It is so simple. After creating function you can use this code just like the way:
      if(isMobileDevice()){
      echo “It is a mobile device”;
      }
      else {
      echo “It is desktop or computer device”;
      }

  2. lirik laban says:

    Dear, informerfrk

    I want to make an ads widget but it’s just shown on Mobile display only. And not shown on the desktop. How can I do it, I tried to make as you tell above but, the ads appear on the desktop too…

    please

    • Faruque Ahamed Mollick Faruque Ahamed Mollick says:

      Hello lirik, I can’t see your code. Please check your code again. Also, be sure that your desktop is not in the mobile device mode or your mobile browser is not in desktop mode.

Leave a Reply

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