Mobile device detection in PHP


Now all web projects have a desktop version site as well as mobile version site. When the visitors visit your website from a mobile device, they need to be redirected to the mobile website. We have made a simple user agent class for detect mobile devices and tablets in PHP. Also you would be able to redirect mobile users to the mobile site with our user agent class.

The user agent class is simple PHP class which helps to mobile device detection. You should call a method from this class to check if visitors visit your site from mobile devices or tablets or desktop and you can redirect visitor to the suitable website.

You only need to download two files from below. user_agents_config.php file contains all the user agents and user_agent.php file contain UserAgent class. Also note that, those two files should insert at the same directory.

At first you will need to include the user_agents_config.php file and create an instance of UserAgent class. Now just call the is_mobile() function for checking if the visitors from mobile devices. is_mobile() function return TRUE if the visitors from mobile devices, otherwise return FALSE.

<?php
//include file
include_once 'user_agent.php';

//create an instance of UserAgent class
$ua = new UserAgent();

//if site is accessed from mobile, then redirect to the mobile site.
if($ua->is_mobile()){
    
header("Location:http://m.codexworld.com");
    exit;
}
?>

You can place the above PHP code to the beginning of the landing page at your desktop version website.

6 Comments

  1. Robert Said...
    • CodexWorld Said...
  2. Ruben B. Said...
  3. Ruben B. Said...
  4. Yogendra Said...
  5. Alessandro Said...

Leave a reply

Connect With CodexWorld