PHP – Detect mobile device and do redirection

We can use PHP to detect the browser user agent such that it would redirect user to another URL if the request comes from a mobile device.

1. Create the user_agent.php.

$iphone  = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry   = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod    = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");

if ($iphone || $android || $palmpre || $ipod || $berry == true) { 
  echo "<script>window.location=''</script>";


Include the user_agent.php inside those views PHP where you want to redirect the visitor if they use a mobile.

<?php include('user_agent.php'); ?>
<!-- HTML below-->
<!DOCTYPE html>
    <meta charset="UTF-8" />


Done =)

Reference: 9Lessons – Redirect Mobile Devices with PHP


3 thoughts on “PHP – Detect mobile device and do redirection”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.