How to Detect Mobile Browser And Redirect Mobile Users To Mobile Website In PHP


We all want to give better user experience to our website visitors. So it is nice to have the mobile friendly version of our websites. This is also recommended as the full version of the website takes enough time to load and users also have problems in navigating the websites on mobile devices. So it is a nice idea to have a mobile-friendly version of the website and redirect mobile visitors to the mobile version of the website.

To do this we use $_SERVER[‘HTTP_USER_AGENT’] PHP variable. It returns the browser information.

Most common mobile OS are

  • Android
  • iPhone
  • BlackBerry
  • iPad

So here is the code to check for these mobile users.



I wrote this code long ago. But, we have better and more effective options now. Now you can us available libraries for easier and effective implementation. Try MobileDetect PHP library.

It is a lightweight PHP class for detecting mobile devices. It combines User-Agent string with HTTP headers to detect the mobile environment. Best thing is that you can individually identify the type of mobile device like smartphone, tablet, Android, iOS and more.

You only need to include the PHP file and create the object of the class to use it.

There are various other functions to make it better and more useful. You can functions like

isTablet() , isiOS() and isAndroidOS(). You can also detect browser by using codes like:

I personally recommend the use of this library for better mobile device detection using PHP.



To Top