Quantcast
Channel: Eureka! » PHP
Viewing all articles
Browse latest Browse all 16

PHP – Detect mobile device and do redirection

$
0
0

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.

<?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) { 
  header('Location: http://eureka.ykyuen.info');
  //OR
  echo "<script>window.location='http://eureka.ykyuen.info'</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>
<html>
  <head>
    <meta charset="UTF-8" />
  </head>
  <body></body>
</html>

 

Done =)

Reference: 9Lessons – Redirect Mobile Devices with PHP


Filed under: PHP Tagged: PHP

Viewing all articles
Browse latest Browse all 16

Trending Articles