As we all know 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. Today I am going to show you that How to detect device and redirect user on compatible site.
We 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 or download our demo which also contain that class and live demo files.
PHP Code:
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:
<?php
if( $detect->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.