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)

Create a file with the following script and name it as user_agent.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: /');
echo "<script>window.location=''</script>";

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

include('user_agent.php');  // Redirecting
// code for page

Have something to Add?

Loading Facebook Comments ...
Loading Disqus Comments ...