All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Detect And Redirect To Mobile Site Using PHP

Last Updated : Jul 1, 2023

IN - PHP | Written & Updated By - Ashish

In this tutorial we will show you how to detect and redirect to a mobile site using PHP, Mobile Site is one of the huge transformation we had seen in web due to the increasing number of mobile devices and of different sizes it becomes difficult to show appropriate version of website to the user.

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

You may also like redirect webpage after delay.

Detect And Redirect To Mobile Site Using PHP

To Detect And Redirect To Mobile Site It Takes Only Two Steps:-

  1. Make a PHP file and define markup for sample webpage
  2. Make a PHP file and define code to detect and redirect

Step 1. Make a PHP file and define markup for sample webpage

We make a PHP file and save it with a name detect_redirect.php

<?php
require_once 'redirect.php';
?>

<html>
<body>
<div id="wrapper">
 <h1>Detect And Redirect To Mobile Site Using PHP</h1>
</div>
</body>
</html>

In this step we create a sample page and add redirect.php page which is the main page used to detect and redirect to mobile site.

You may also like detect adblocker using JavaScript.

Step 2. Make a PHP file and define code to detect and redirect

We make a PHP file and save it with a name redirect.php

<?php
$detect_device = new Mobile_Detect;

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

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

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

In this step there are two ways to detect you can use global method which is isMobile and isTablet to detect mobile and tablet devices and redirect to mobile site.

You can also use specific styling for specific operating system like isAndroidOS is used to detect whether the user operating system is android or not if yes it use different css for android devices same as for ios and windows.

You may also like detect url in input using jQuery.

Thats all, this is how to detect and redirect to mobile site using PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on php mobile detect and redirect to mobile site helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪