Redirecting mobile devices to another page best practise I am redirecting mobile devices from the root index.php to mobile.php file. I only allow access to mobile.php file only when that if statement is true.

This is how I do it:


Detect Devices and store them into variables
If a mobile device (see variables) accesses the web application redirect to mobile.php
In mobile.php check again the same thing and redirect back to index.php if the device is not a mobile device.


  Is this a good practice to redirect mobile devices to another file?




  Is there any tool to check my script from false positives?


index.php

    &lt;?php #detecting mobile devices
    $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");
    $ipad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");

    if($iphone || $android || $palmpre || $ipod || $berry || $ipad == true) 
{   
 header('Location: mobile.php');
 exit;
}
    ?&gt;


mobile.php

&lt;?php #detecting mobile devices
$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");
$ipad = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");


if($iphone || $android || $palmpre || $ipod || $berry || $ipad == true) 
{  
//do nothing
} 
else{ #device not one of the variables return to root file
header('Location: index.php');
exit;
}

 ?&gt;
The global task of mobile detection aside, just in regard of the current implementation. 

Every time you see a duplicated code, there is a room for the improvement. 

$mobile = false;
$mob_agents = ["iPhone","Android","webOS","BlackBerry","iPod","iPad"];
foreach ($mob_agents as $marker) {
if (strpos($_SERVER['HTTP_USER_AGENT'], $marker) !== FALSE) {
    $mobile = true;
    break;
}


now you can use a $mobile variable to test whether it's a movile client or not

A more sophisticated version would involve basic regular expressions- something like this:

$pattern = "/iPhone|Android|webOS|BlackBerry|iPod|iPad/";
$mobile = preg_match($pattern, $_SERVER['HTTP_USER_AGENT']);