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.
Leave a Reply to Vlad Cancel reply