Are there any best practices for detecting and redirecting mobile devices in PHP?
When detecting and redirecting mobile devices in PHP, it is best practice to use user-agent detection to identify mobile devices. Once a mobile device is detected, you can then redirect the user to a mobile-friendly version of your website or a specific mobile page.
$mobile_agents = array("iPhone","iPad","Android","webOS","BlackBerry","Windows Phone");
function isMobileDevice(){
$user_agent = $_SERVER['HTTP_USER_AGENT'];
global $mobile_agents;
foreach($mobile_agents as $agent){
if(strpos($user_agent, $agent) !== false){
return true;
}
}
return false;
}
if(isMobileDevice()){
header("Location: mobile-friendly-page.php");
exit();
}