Like Us on Facebook

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);
}
?>

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. 

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 0 )

    Leave a comment..

    Click to login.