{"id":96,"date":"2011-12-02T07:04:57","date_gmt":"2011-12-02T15:04:57","guid":{"rendered":"http:\/\/shanelabs.com\/blog\/?p=96"},"modified":"2011-12-21T07:29:52","modified_gmt":"2011-12-21T15:29:52","slug":"mobile-phone-vs-tablet-detection-in-php","status":"publish","type":"post","link":"https:\/\/shanelabs.com\/blog\/2011\/12\/02\/mobile-phone-vs-tablet-detection-in-php\/","title":{"rendered":"Mobile Phone vs. Tablet Detection in PHP"},"content":{"rendered":"<p>Since posting\u00a0<a title=\"Simple Mobile Browser Detection in PHP\" href=\"http:\/\/shanelabs.com\/blog\/2011\/11\/29\/simple-mobile-browser-detection-in-php\/\">Simple Mobile Browser Detection in PHP<\/a>\u00a0I have received feedback indicating that an iPad is not considered a mobile device. It\u00a0ultimately\u00a0depends on your definition of &#8216;mobile&#8217;, 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&#8217;d want to distinguish between them.<\/p>\n<p>So I updated my code to detect just mobile phones:<\/p>\n<pre>function is_mobile_phone()\r\n{\r\n\t$ua = strtolower($_SERVER['HTTP_USER_AGENT']);\r\n\t$mobiles = array(\"iphone\", \"ipod\", \"blackberry\", \"nokia\", \"phone\",\r\n\t\t\"mobile safari\", \"iemobile\");\r\n\tforeach($mobiles as $mobile)\r\n\t{\r\n\t\tif(strpos($ua,$mobile)) return true;\r\n\t}\r\n\treturn false;\r\n}<\/pre>\n<p>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 &#8216;mobile&#8217; with &#8216;mobile safari&#8217; and &#8216;iemobile&#8217;. <a href=\"http:\/\/googlewebmastercentral.blogspot.com\/2011\/03\/mo-better-to-also-detect-mobile-user.html\">Google<\/a> had pointed out that it&#8217;s best to look for android AND mobile, but it&#8217;s just as easy to search for &#8216;mobile safari&#8217;. Most cell phones have a string similar to:<\/p>\n<blockquote><p>&#8220;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 <strong>Mobile Safari<\/strong>\/533.1&#8243;<\/p><\/blockquote>\n<p>Whereas most tablets have a string resembling:<\/p>\n<blockquote><p>&#8220;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 <strong>Mobile<\/strong>\/7B367 <strong>Safari<\/strong>\/531.21.10&#8243;<\/p><\/blockquote>\n<p>I came across a couple user agent strings that don&#8217;t follow these rules, but weren&#8217;t very popular devices. This should cover 95% of the cases.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since posting\u00a0Simple Mobile Browser Detection in PHP\u00a0I have received feedback indicating that an iPad is not considered a mobile device. It\u00a0ultimately\u00a0depends on your definition of &#8216;mobile&#8217;, 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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,6,4],"tags":[],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-software","category-tech","category-web"],"_links":{"self":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/comments?post=96"}],"version-history":[{"count":8,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/96\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}