Nov 292011
 

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!

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)