How To Redirect Mobile Users To Your Mobile Website

The PHP command

$_SERVER['HTTP_USER_AGENT']

returns browser information, and we can get a list of all mobile browsers from this website.

Now if we only had a function we could use to know if the browser’s device is mentioned in $_SERVER[‘HTTP_USER_AGENT’], ah we do it’s

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

This function finds the needle in the hay stack and returns the position in the string, which could start at zero. Let’s make a list of mobile OS’:

Android
iPhone
BlackBerry
iPad

So then the script you need is:

$android=strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$iphone=strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$bb=strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipad=strpos($_SERVER['HTTP_USER_AGENT'],"ipad")

if($android || $iphone || $bb || ipad >=0)
{
// redirect to mobile site
}
// show full site