The simple way to Detect Mobile Device in PHP

You may need to detect a mobile device on a PHP based website in the project.

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 the 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, it will not prevent 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 a 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?

Now if you able to detect the mobile device in PHP, then you can also detect desktop just using the if else statement again. In the above code what you can see in else part is for a desktop device.

5 responses to “The simple way to Detect Mobile Device in PHP”

  1. p1clem says:

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

    • 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 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.

  3. Fred McKinney says:

    Lirik, the code that displays the ads should be there within the brackets immediately after the IF part of the statement. Having an ELSE conditional is not a requirement. Thus, if your code displaying the ads is in the brackets immediately following the IF statement and one is viewing your page on a desktop or laptop, then the code for displaying the ads is ignored. Or have you tried that? Displaying what code you have here would help.

Leave a Reply

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