In developing TaskShot, I wanted to determine which users were using their cell phones to browse the site. A quick search yielded many large classes, bloated snippets, and code that did way too much. All I needed was to detect if they were coming from a mobile phone or not.
I figured the simplest way would be to check the user-agent header for a few keywords. I got this list of keywords after browsing through this recent list of mobile browser agents
Here’s what I came up with:
function is_mobile()
{
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
$mobiles = array("android", "iphone", "ipod", "ipad",
"blackberry", "palm", "mobile", "mini", "kindle");
foreach($mobiles as $mobile)
{
if(strpos($ua,$mobile)) return true;
}
return false;
}
I’ve collected lots of data so far and it seems to be pretty accurate, but as always let me know if you have any improvements!
About Shane
Shane is the creator of task management system TaskShot and musician's helper SongKeyFinder
Shane is the creator of task management system TaskShot and musician's helper SongKeyFinder