35

Possible Duplicate:
Simplest way to detect a mobile device

I 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?

4
  • not sure if this is too much for what you need, but you may want to have a look: wurfl.sourceforge.net/nphp Commented Jun 29, 2011 at 17:01
  • see: mobiledetect.net seems the simplest way... Commented Jun 5, 2013 at 15:34
  • Dough in contrast this one is easier to include and actually works !! i'd sugest you vote for @iamandrus as answer Commented Sep 4, 2014 at 21:21
  • You could use a third party api service like useragentinfo.co or see my answer here stackoverflow.com/a/44982837/395676. It could detect browser version, OS version and device type. Commented Jan 30, 2018 at 2:36

2 Answers 2

56

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'; } ?> 
Sign up to request clarification or add additional context in comments.

7 Comments

nice implementation. Kudos!
This 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.
in 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 !
Oh 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)
using to standard android browser and its not working!
|
4

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.

1 Comment

Might be helpful to show or link to an example of using WURFL in PHP to accomplish what the @Gromdroid wants.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.