php mobile detection
mobile browser detection made easy
I wanted to get my hands on a good mobile detection PHP script and, after playing with a couple, I decided to put together my own.
First off it’s worth mentioning the two main resources I used:
- http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/
- http://www.andymoore.info/php-to-detect-mobile-phones/
Andy Moore’s script is one of the big hitters out there that a lot of people use, but i had some problems with it.
You can download my fully commented version from http://www.richardshepherd.com/php/isMobile.zip, and here it is in all it’s glory…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | /* isMobile.php A PHP script to detect mobile phones. v1.1 :: 23 September 2008 Check out my blog http://www.richardshepherd.com Or subscribe to my RSS feed http://feeds.feedburner.com/RichardShepherd */ function isMobile() { // Check the server headers to see if they're mobile friendly if(isset($_SERVER["HTTP_X_WAP_PROFILE"])) { return true; } // If the http_accept header supports wap then it's a mobile too if(preg_match("/wap.|.wap/i",$_SERVER["HTTP_ACCEPT"])) { return true; } // Still no luck? Let's have a look at the user agent on the browser. If it contains // any of the following, it's probably a mobile device. Kappow! if(isset($_SERVER["HTTP_USER_AGENT"])){ $user_agents = array("midp", "j2me", "avantg", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows ce", "mmp/", "blackberry", "mib/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up.b", "audio", "SIE-", "SEC-", "samsung", "HTC", "mot-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "dddi", "moto"); foreach($user_agents as $user_string){ if(preg_match("/".$user_string."/i",$_SERVER["HTTP_USER_AGENT"])) { return true; } } } // Let's NOT return "mobile" if it's an iPhone, because the iPhone can render normal pages quite well. if(preg_match("/iphone/i",$_SERVER["HTTP_USER_AGENT"])) { return false; } // None of the above? Then it's probably not a mobile device. return false; } // Here's our code to which calls the above function if (isMobile()) { // if the function returned true, it's a mobile. echo "mobile"; // delete this line in your code, and uncomment the next line // header('Location: http://www.yoursite.mobi/'); // let's redirect the page to the mobile site } else { // if not, we're in a standard browser and our page // can proceed as normally formatted for the web! echo "web"; // delete this line in your code } |
And that’s your lot. It seems to work for me under multiple browser conditions. If you have any problems, let me know.





