Mobile Browser Detection and Redirection using PHP

      Comments Off on Mobile Browser Detection and Redirection using PHP

This is a very simple script to detect the mobile browser and redirect the customers to the mobile version of the website. It is very easy to use and is of just few lines of code.

$_SERVER – an array containing information such as headers, paths, and script locations. (Know more)

user_agent.php
Create a file with the following script and name it as user_agent.php

<?php
$isMobile = (bool)preg_match(‘#\b(ip(hone|od|ad)|android|opera m(ob|in)i|windows (phone|ce)|blackberry|tablet’ . ‘|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp|laystation portable)|nokia|fennec|htc[\-_]’ . ‘|mobile|up\.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\b#i’, $_SERVER[‘HTTP_USER_AGENT’] );
if ($isMobile)
{
header(‘Location: http://m.example.com /’);
//OR
echo “<script>window.location=’http://m.example.com'</script>”;
}
?>

in index.php or home.php
You have to include the above script like this.

<?php
include(‘user_agent.php’);  // Redirecting http://m.example.com
// code for example.com page
?>

Author: Teji

i am admin