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…
/*
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.
Pingback: Mobile Detection & Optimization | Reverb Studios Blog
Rich,
Thanks so much for this code. It’s exactly what I was looking for. Is it possible to do with a WP site? I’m having a hard time redirecting. Have you had any experience with this? Thanks again!
Hi! I had a problem with this script. I had to change the line…
if(preg_match(“/”.$user_string.”/i”,$_SERVER["HTTP_USER_AGENT"])) {
to change the delimiters from / to @. Works OK now.
The problem is/was that some of the user agent strings (in the array)
themselves contain a /.
Andrew
Hi, i have some question about this function, i hope you can help me:
Why you don’t use strpos instead of preg_match?
This script I can actually read. But being a newbie and working with html, I am still a bit challenge. A couple questions I have are:
1. Can you still use index.html or do you need index.php
2. If using html do you need a .htaccess file with the following in it?
AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm
AddHandler x-httpd-php .html .htm .cgi .php
3. Does the script go before all tags (header and body)
4. When you say delete this line, I am not quite sure how much to delete and if comment is just the URL ( I know I am so close and your script makes the most sense)
Thanks in advance!
If your still interested…
– 1. I would have thought you have change the apahce config file to allow PHP to run on .html but I’m not 100% sure if that will work.
– 2. I’m not sure? – Pretty much the same as question 1.
– 3. Lines 1-47 are all appart of the function declaration, so ideally it should be included at the top of your page.
- 4. You delete lines 52 & 60, as these just display “web” and “mobile” to the browser for testing purposes.
Pingback: mobile browser detection made easy | Lâm Trường Huy
Thanks for the post and the code. I am new to PHP and this saved me mucho time. It works nicely EXCEPT….it strips added url tags. I am using Google Adwords and Google Analytics to send traffic to my site and analyze the activity. Analytics adds tags to the end of the url so that it can track what pages are visited, when, and by who. The URL request from the browser is in this form: http://www.mysite.com/?gclid=123xyz
The dynamic gclid tag gets lost in the redirect. Do you know of any way to capture the tag and append to the target url?
Thanks
Hi I was wondering if you could help..
I have this in the code
////////////
// 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;
}
////////////
Which doesn’t seem to work for me as both the iPhone and iPad are seen as mobile devices. But what I actually want is only the iPhone to be seen to be a “phone” and the iPad to be seen as a normal web page. Is there the ability for that to happen?
Any help greatly appreciated.
The same for me. Didi you find the answer?
No reply as yet – can anybody else help?
I would also like to do this
Rich you are one sexy motherflumper! Thanks man.