Detect mobile browser
Solution 1
Have my user agent code:
<?php
/* USER-AGENTS
================================================== */
function check_user_agent ( $type = NULL ) {
$user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );
if ( $type == 'bot' ) {
// matches popular bots
if ( preg_match ( "/googlebot|adsbot|yahooseeker|yahoobot|msnbot|watchmouse|pingdom\.com|feedfetcher-google/", $user_agent ) ) {
return true;
// watchmouse|pingdom\.com are "uptime services"
}
} else if ( $type == 'browser' ) {
// matches core browser types
if ( preg_match ( "/mozilla\/|opera\//", $user_agent ) ) {
return true;
}
} else if ( $type == 'mobile' ) {
// matches popular mobile devices that have small screens and/or touch inputs
// mobile devices have regional trends; some of these will have varying popularity in Europe, Asia, and America
// detailed demographics are unknown, and South America, the Pacific Islands, and Africa trends might not be represented, here
if ( preg_match ( "/phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {
// these are the most common
return true;
} else if ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap /", $user_agent ) ) {
// these are less common, and might not be worth checking
return true;
}
}
return false;
}
?>
How to use:
<?php
$ismobile = check_user_agent('mobile');
if($ismobile) {
return 'yes';
} else {
return 'no';
}
?>
Solution 2
I wrote this script to detect a mobile browser in PHP.
The code detects a user based on the user-agent string by preg_match()ing. It has 100% accuracy on all current mobile devices and I'm currently updating it to support more mobile devices as they come out. The code is called isMobile and is as follows:
function isMobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
You can use it like this:
// Use the function
if(isMobile())
// Do something for only mobile users
else
// Do something for only desktop users
To redirect a user to your mobile site, I would do this:
// Create the function, so you can use it
function isMobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
// If the user is on a mobile device, redirect them
if(isMobile())
header("Location: http://m.yoursite.com/");
Let me know if you have any questions and good luck!
Solution 3
At work, we use WURFL - there are millions of different browsers out there, and you're better of to re-use the work that others with experience did in that regard than implementing your own solution.
Gromdroid
Updated on June 12, 2020Comments
-
Gromdroid almost 4 years
Possible Duplicate:
Simplest way to detect a mobile deviceI have a site and I want to detect which browser is used and redirect them. I have a php index and the code must be in php. I've found many sites but they don't work or they don't detect many mobile browsers. Do you know of any good code or tutorials that can detect many mobile browsers?
-
Gromdroid almost 13 yearsHey, Thank you very much. This really help for me.
-
cdmckay over 12 yearsMight be helpful to show or link to an example of using WURFL in PHP to accomplish what the @Gromdroid wants.
-
Yinda Yin almost 12 years@AndrewBarber: Seems as if it meets all of the requirements outlined here
-
Andrew Barber almost 12 years@robert ok. my thing was that the answers are pretty much duplicates, but I understand :)
-
Justin DoCanto almost 12 yearsi dont understand what the issue is. multiple questions asking how to redirect in PHP, so i answered. would do you guys suggest i do instead?
-
vlex almost 11 yearsnice implementation. Kudos!
-
Mladen Janjetovic over 10 yearsThis script creates problem with mine IE8. It returns "Tablet PC 2.0" in user agent and this script detect him as mobile whitch is wrong
-
Matt about 10 yearsThis code still works. But some things have changed. Apple iPhone puts Mozilla in front of their user agent now. So first check mobile and then desktop.
-
user613326 over 9 yearsin the line for mobile, add a |Mobile| to detect firefox mobile on android. Besides great script finally i found a true php one that worked thumbs up, kuddos !
-
user613326 over 9 yearsOh and if some device isnt working just ad an echo $user_agent; to find its name. somehow i noticed the script halts when it couldnt detect (firefox)
-
Doug over 9 yearsDead link. Please update.
-
Ron over 9 yearsThis works SO well. THANKS!!!!!
-
Aryeh Armon almost 9 yearsusing to standard android browser and its not working!
-
Abela over 8 yearsHow do we keep MS Edge from being detected as mobile?
-
mindmyweb over 8 yearsSays its desktop on iphone 6 even after i put mobile first