Mobile Browser Detection With PHP
You’ll find (or maybe you might not) that there are several ways to detect what mobile browser, if any, is currently being used to access a website, but the simplest is just to use regular expressions to parse the HTTP_USER_AGENT variable. I’ll demonstrate simple mobile browser detection using PHP, and to make things even more pleasant, we’ll only be trying to detect if the user is browsing on an iPad.
$accept = $_SERVER['HTTP_ACCEPT'];
$agent = $_SERVER['HTTP_USER_AGENT'];
$target = 'ipad'
if(preg_match('/ipad/i', $agent)) {
echo "All hail Mapple!";
} else {
echo "Wouldn't you like to be on the bandwagon? Come drink the Kool-Aid!";
}
Now that you know how the core of this is going to work, here is a function that will detect what mobile browser is being used, or return false if it’s not a mobile browser.
function detectmobile() {
$accept = $_SERVER['HTTP_ACCEPT'];
$agent = $_SERVER['HTTP_USER_AGENT'];
$result = false;
switch($agent) {
case preg_match('/ipad/i', $agent):
$result = 'iPad';
break;
case preg_match('/(iphone|ipod)/i', $agent):
$result = 'iPhone / iPod Touch';
break;
case preg_match('/android/i', $agent):
$result = 'Android';
break;
case preg_match('/blackberry/i', $agent):
$result = 'Blackberry';
break;
case preg_match('/(pre\/|palm)/i', $agent):
$result = 'Palm';
break;
}
return $result;
}
By the way, if you thought Mapple was a typo, I’m actually referring to the Simpsons episode Mypods and Boomsticks.



