Thursday, March 14, 2013

Redirect to mobile devices using PHP

Redirect to mobile devices using PHP


To be sure your site is ready and sized correctly you may want to detect and serve up alternate design/layouts for popular sizes and resolutions in which those devices view web pages. You can use PHP code on your homepage to run mobile device detection and redirection. Target any or all devices that may come into play as popular web browsing tools. Usining strpos() function find position of first occurrence of a string, here full php code :
// find iphone occurence on HTT_USER_AGENT variable
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
// find android occurence on HTT_USER_AGENT variable
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
// find webos occurence on HTT_USER_AGENT variable
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
// find blackberry occurence on HTT_USER_AGENT variable
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
// find ipod occurence on HTT_USER_AGENT variable
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");

if ($iphone || $android || $palmpre || $ipod || $berry == true)
{
header('Location: http://your-mobile-site-here.com/');
}

Disqus Comments