{"id":89,"date":"2011-11-29T18:25:23","date_gmt":"2011-11-30T02:25:23","guid":{"rendered":"http:\/\/shanelabs.com\/blog\/?p=89"},"modified":"2011-12-15T21:37:52","modified_gmt":"2011-12-16T05:37:52","slug":"simple-mobile-browser-detection-in-php","status":"publish","type":"post","link":"https:\/\/shanelabs.com\/blog\/2011\/11\/29\/simple-mobile-browser-detection-in-php\/","title":{"rendered":"Simple Mobile Browser Detection in PHP"},"content":{"rendered":"<p>In developing <a title=\"Task Shot Management\" href=\"http:\/\/www.taskshot.com\">TaskShot<\/a>, 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.<\/p>\n<p>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 <a href=\"http:\/\/www.zytrax.com\/tech\/web\/mobile_ids.html\">list of mobile browser agents<\/a><\/p>\n<p>Here&#8217;s what I came up with:<\/p>\n<pre>function is_mobile()\r\n{\r\n\t$ua = strtolower($_SERVER['HTTP_USER_AGENT']);\r\n\t$mobiles = array(\"android\", \"iphone\", \"ipod\", \"ipad\",\r\n\t\t\"blackberry\", \"palm\", \"mobile\", \"mini\", \"kindle\");\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&#8217;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!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&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,4],"tags":[],"class_list":["post-89","post","type-post","status-publish","format-standard","hentry","category-software","category-web"],"_links":{"self":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/89","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=89"}],"version-history":[{"count":7,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":122,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/89\/revisions\/122"}],"wp:attachment":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}