How to detect a mobile device and redirect to a mobile website using PHP
Hello, friends!!! Today our topic about detect a mobile device and redirect to a mobile website using PHP. Now a days we have seen every organization has a desktop version website as well as mobile version website. Mobile users are increased day by day. So it is very important to use mobile version website too.
Many ways you can redirect your desktop website to a mobile website. In this tutorial, we are going to do same things but using PHP.
Here we using Mobile Detect class to detect any mobile devices and redirect to your mobile website. For redirection, you need to create a folder suppose m is the folder and you have to paste your all mobile website files.
Main detection code will add in index.php file of a desktop website. Only you need to include mobile_detect_page.php file. Because this file contains Mobile Detect class.
<?php
include 'mobile_detect_page.php';
$detect = new Mobile_Detect();
if ($detect->isMobile()) {
header('Location: m/index.php');
exit(0);
}
?>
include 'mobile_detect_page.php';
$detect = new Mobile_Detect();
if ($detect->isMobile()) {
header('Location: m/index.php');
exit(0);
}
?>
In the above code, first you have to include mobile_detect_page.php file for Mobile Detect class to detect any mobile devices. When you include mobile_detect_page.php then automatically create an instance of Mobile Detect class. Now isMobile() function will check is the visitor from mobile device or desktop device. If mobile device is true then isMobile() function will redirect to the mobile website folder otherwise if it is false. Then isMobile() function will not redirect any page. So it will stay on Desktop website.
But you have to remember one thing which is important that is above code will must have to paste on the top of the index page (Desktop website's index page) .
Also you can download this file and view demo. If you have any question in mind then please comment below we will happy to answer.
![]() |
Live Demo |
|

You may also like
Comments ( 0 )
Subscribe Latest Information
Categories
Most Popular Posts
How to Withdraw Money from ATM Machine 7steps 1178778 Views
How to Create Chat Application in Android Studio 151635 Views
How to Create a Shopping Cart Application in Android 114496 Views
You May Like Also
How to Send Mail with HTML Template using PHP 12313 Views
How to Integrate CKEditor in Web Page using PHP 13137 Views