How to Detect Mobile Browsers and Redirect site in PHP
Arjun JS
Nowadays responsive designs are must for better display of the websites and all prefer to use it. So in this, We have used a PHP class Mobile-Detect it uses the User-Agent string combined with specific HTTP headers to detect the mobile environment. Get this class file from GitHub.
index.php Contains PHP code to detect device and redirect if the 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.getcodify.com/');//your website version url
exit;
}
if( $detect->isTablet()) {detects your tablets
header('Location: http://m.getcodify.com/');//your website version url
exit;
}
if( $detect->isMobile() && !$detect->isTablet()) { // Exclude tablets devices and use for mobile only.
header('Location: http://m.getcodify.com/');//your website version url
exit;
}
if( $detect->isAndroidOS()) {// detects your Android platforms
header('Location: http://m.getcodify.com/');//your website version url
exit;
}
if( $detect->isiOS()) {// detects your iOS platforms
header('Location: http://m.getcodify.com/');//your website version url
exit;
}
if( $detect->isWindowsPhoneOS()) {// detects your Windows Phone platforms
header('Location: http://m.getcodify.com/');//your website version url
exit;
}
// and many other browsers you can filter
?>
This will redirect a user to website’s mobile version if a user coming from a mobile device.