Its a matter of well known fact that most of the peoples use mobiles and tablets instead of using computer all the day and as a web developer / designer you need to create your websites compatible to all devices with responsive designs or you can simply create different sites for mobile and tablet users on a subdomain with compatibility. The reason behind all this is just too simple, reason is there are thousands of potential customers out there waiting for you with hand held devices , so its important to grab them by providing what its ought to.
Today I am going to show you that How to detect device and redirect user on compatible site.
I have used a PHP class Mobile-Detect it uses the User-Agent string combined with specific HTTP headers to detect the mobile environment. You can get this class from github
index.php Contains php code to detect device and redirect if device is mobile or tablet.
< ?php require_once 'Mobile_Detect.php'; // PHP Class to detect device. $objMobile = new Mobile_Detect; if($objMobile ->isMobile()) {
header('Location: http://m.yoursite.com/');
exit;
}
?>
Above code will redirect user to website’s mobile version if user coming from a mobile device.
Some other cases to redirect website:
isTablet()) {
// Redirect or operate any tablet device.
}
if( $detect->isMobile() && !$detect->isTablet()) {
// Exclude tablets devices and used for mobile only.
}
if( $detect->isAndroidOS()) {
// Check for Android platforms
}
if( $detect->isiOS()) {
// Check for iOS platforms
}
if( $detect->isWindowsPhoneOS()) {
// Check for Windows Phone platforms
}
// many more like browser's etc you can filter
?>
Above code shows you more specific checks for device you user using so I hope this tutorial helps you and please dont forget to share it with your friends and do comment you issues and suggestion for our upcoming articles.
Piyush Rishi Singh
www.piyushrishisingh.com
A Digital Entrepreneur & Content Creator who loves simplifying tech.
Expertise: A Full Stack & highly skilled 10+ years experienced WordPress developer who specializes in complete custom tailored WordPress Websites development.