How to Detect and Redirect to Mobile Site using PHP

We provide a few cases below which describe how to use mobile-detect to alter your site behavior. In this tutorial i will show to simple script how to detect and redirect to mobile site using PHP.

There is a PHP class used for detecting mobile devices Mobile Detect we use this class to detect the mobile devices.

HTML and PHP Code

<?php
require_once 'mobile.php';
?>
<html>
    <body>
        <h1>How to Detect and Redirect to Mobile Site using PHP</h1>
    </body>
</html>

PHP Code (mobile.php)

<?php
$detect_device = new Mobile_Detect;

// For All Mobile Devices
if($detect_device->isMobile()) 
{
    header('Location: http://m.dome.com/');
    exit;
}

// For All Tablet Devices
if( $detect_device->isTablet()) 
{
    header('Location: http://m.dome.com/');
    exit;
}

// For Specific Operating System
if( $detect_device->isAndroidOS()) 
{
    echo "<link rel='stylesheet' href='css/android.css' type='text/css' />";  
}
if( $detect_device->isiOS()) 
{
    echo "<link rel='stylesheet' href='css/ios.css' type='text/css' />"; 
}
if( $detect_device->isWindowsPhoneOS()) 
{
    echo "<link rel='stylesheet' href='css/windows.css' type='text/css' />";  
}

Leave a Reply

Your email address will not be published. Required fields are marked *