Simple PHP Mobile Website Redirect Code

By May 28, 2013 February 27th, 2015 Technology

Here is a simple PHP mobile website redirect that will work for most PHP websites in a linux environment. The even works when added to the header.php file in most WordPress themes.

[code]
<?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=’http://m.site.com'</script>”;
 }
?>
[/code]