ad

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.

$android=strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$iphone=strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$bb=strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipad=strpos($_SERVER['HTTP_USER_AGENT'],"ipad")

if($android || $iphone || $bb || ipad >=0)
{
// redirect to mobile version of your website
}
// show full site

Update

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.

require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
// Any mobile device (phones or tablets).
if ( $detect->isMobile() ) {
 
}

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:

$detect->is('Chrome')
$detect->is('iOS')

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

 

Tags: |
Nord VPN Sale

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.


Similar Articles

1 Comments

  • Best Walkie Talkie says:

    Interesting blog! Is your theme custom made or did you download it
    from somewhere? A design like yours with a few simple tweeks would really make
    my blog jump out. Please let me know where you got your design.

    Thank you

  • Best Walkie Talkie says:

    Interesting blog! Is your theme custom made or did you download it
    from somewhere? A design like yours with a few simple tweeks would really make
    my blog jump out. Please let me know where you got your design.

    Thank you

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip | Developed By Deepanker