Since posting Simple Mobile Browser Detection in PHP I have received feedback indicating that an iPad is not considered a mobile device. It ultimately depends on your definition of ‘mobile’, but I see their point in that tablets are very different from cell phones. Screen size and browser capability are pretty different between mobile phones and tablet/pads, so I can understand why you’d want to distinguish between them.

So I updated my code to detect just mobile phones:

function is_mobile_phone()
	$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
	$mobiles = array("iphone", "ipod", "blackberry", "nokia", "phone",
		"mobile safari", "iemobile");
	foreach($mobiles as $mobile)
		if(strpos($ua,$mobile)) return true;
	return false;

I removed ipad and kindle since those are easy to detect. I removed android, palm, and mini since some of those can be tablets. I added IEMobile for Windows phones. But the most significant change is replacing ‘mobile’ with ‘mobile safari’ and ‘iemobile’. Google had pointed out that it’s best to look for android AND mobile, but it’s just as easy to search for ‘mobile safari’. Most cell phones have a string similar to:

“Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; T-Mobile G2 Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1″

Whereas most tablets have a string resembling:

“Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10″

I came across a couple user agent strings that don’t follow these rules, but weren’t very popular devices. This should cover 95% of the cases.

About Shane
Shane is the creator of Page Speed Analytics tool MachMetrics. You can follow his entrepreneurial activities on his blog SideProjectProfit

  4 Responses to “Mobile Phone vs. Tablet Detection in PHP”

  1. I should note that screen size might be better for your intended detection use, but there’s no way to get that via PHP – have to use client site javascript.

    • Nah, that’s no good either. My Samsung Galaxy Nexus has a bigger screen resolution than most tablets, including the original iPad. I still want to be served the mobile version of sites, not the desktop.

      • Your galaxy nexus has a better resolution but the has bigger pixel density, google it, you’ll see that in CSS for example a Nexus 5 has only 360px in width (1080px with a pixel density of 3).

  2. Wow, thank you! Really nice, exactly what I needed.

 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>



© 2012 ShaneLabs Suffusion theme by Sayontan Sinha