Standard way to detect mobile browsers in a web application based on the http request

BrowserMobileEnterpriseWeb Applications

Browser Problem Overview


We are beginning to go down the path of mobile browser support for an enterprise e-commerce webapp (Java/Servlet based). Of course there are many decisions to be made, but it seems to me the cornerstone is to be able to reliably detect mobile browsers, and make decisions on the content to be returned accordingly. Is there a standard way to make this determination (quickly) based on the http request, and ideally glean more information about the given browser and device making the request (screen size, html capabilities, etc?).

I would also appreciate any supplemental information that would be of use from someone who has gone down this path of taking an existing large scale enterprise webapp and architect-ing out mobile browser support from the development side.

[edit] I certainly understand the request header and the information about a database of standard user agents is a great help. For those talking about 'other' request header properties, if you could include similar standardized name / resource of values that would be a big help.

[edit] Several users have proposed solutions that involve a call over the wire to some web service that will do the detection. While I'm sure this works, it is not a good solution for an enterprise e-commerce site for two reasons: 1) speed. A call over the wire for every page request to a third party would have huge performance implications. 2) dependency/legal. We'd tie our website response time and key functionality to their service, which is horrible for legal and risk reasons.

Browser Solutions


Solution 1 - Browser

Wouldn't the standard way be to check the user agent? Here's a database of user agents you can use to detect mobile browsers.

Solution 2 - Browser

@David's answer mentioned using WURFL -- which is probably your best option. Be forewarned, however, the success rate is usually around 60% (from mine and other's experience). With carriers changing UA's constantly and the amount of device profiles that exist (60,000+ ?), there's no bulletproof way to get all the right data you want.

Just a bit of warning before relying heavily on a device DB. I would try to keep the user's options open by allowing them to change session options in case i've guessed wrong.

Solution 3 - Browser

You can use Modernizer to detect browser abilities

Solution 4 - Browser

While you could detect a mobile browser through it's user agent the browser war on the PC platform has shown that sniffing user agents isn't really such a good thing to do.

What ideally should be done is that specific styles should be applied based on media type or that a different answer should be sent based on a header other than the user agent - such as the Accept-header which tells which kind of content that the browser prefers.

Right now it might be enough to code a site that works with the iPhone and with Opera through browser sniffing - but Googles Android is coming any minute now and there are many other mobile phones that will have browser functionality close to the iPhone's in the near future and it would be a waste to develop a mobile website that didn't support those devices as good as possibel from scratch.

Solution 5 - Browser

After days of searching for the right way of detecting a mobile device I've decided to keep it simple [ stupid ] and i shall put a 'Mobile device site' button on my index page.... it's only one click away!!

Solution 6 - Browser

This article (and its follow-up) seems nice.

Solution 7 - Browser

Detect Mobile Browsers - snippets in various programming languages.

Solution 8 - Browser

The following light weight Apache configuration does a pretty good job and remembers user preference if they prefer the PC version

<VirtualHost (your-address-binding)>   

  (your-virtual-host-configuration)       

  RewriteEngine On     
  RewriteCond %{QUERY_STRING} !ui=pc
  RewriteCond %{HTTP_COOKIE} !ui=pc
  RewriteCond %{HTTP_USER_AGENT} "^.*(iphone|ipod|ipad|android|symbian|nokia|blackberry| rim |opera mini|opera mobi|windows ce|windows phone|up\.browser|netfront|palm-|palm os|pre\/|palmsource|avantogo|webos|hiptop|iris|kddi|kindle|lg-|lge|mot-|motorola|nintendo ds|nitro|playstation portable|samsung|sanyo|sprint|sonyericsson|symbian).*$" [NC,OR]

  RewriteCond %{HTTP_USER_AGENT} "^(alcatel|audiovox|bird|coral|cricket|docomo|edl|huawei|htc|gt-|lava|lct|lg|lynx|mobile|lenovo|maui|micromax|mot|myphone|nec|nexian|nook|pantech|pg|polaris|ppc|sch|sec|spice|tianyu|ustarcom|utstarcom|videocon|vodafone|winwap|zte).*$" [NC] 
     
  RewriteRule /(.*) http://bemoko.com/$1 [L]

  RewriteCond %{QUERY_STRING} "ui=pc"
  RewriteRule ^/ - [CO=ui:pc:(your-cookie-domain):86400:/]
  RewriteCond %{QUERY_STRING} "ui=default"
  RewriteRule ^/ - [CO=ui:default:(your-cookie-domain):86400:/]
</VirtualHost>

More background on this @ http://bemoko.com/training.team/help/team/pc-to-mobile-redirect

Solution 9 - Browser

I propose a free detection system which is based on uaprof and user agent: http://www.mobilemultimedia.be UAprof should be the primary key for detection when it's available as there are usually multiple user agents for the same uaprof. If you want to manage this on your own, you should then go for Wurfl because you can download the entire database and manage it locally by yourself.

Solution 10 - Browser

When I had a similar need recently, I found http://www.brainhandles.com/techno-thoughts/detecting-mobile-browsers">this code that uses HTTP_X_WAP_PROFILE, HTTP_ACCEPT, and HTTP_USER_AGENT to identify a browser as mobile or non-mobile. It's PHP but could be converted fairly easily into whatever you need (I implemented it in VBScript for classic ASP).

Ironically, it turned out that I didn't end up using the code because we decided to provide specific URLs for mobile and non-mobile users, but it certainly worked when I was testing it ...

Solution 11 - Browser

You will get most of the information like browser, device, accepted languages, accepted formats etc from the request header. The user agent mentioned above is part of the request header.

Solution 12 - Browser

OK, here is a very simple answer - how about letting the user decide? on your login to your ap, provide a link to the mobile site. on the mobile site, provide a link "back to the main site" - try www.fazolis.com on your mobile device - they do a good job of this.

then, on the link to the mobile site from the browser site, register their "vote" and their user agent. You can build your own reliable list of YOUR clients who want the mobile site. Use this married to specs on screen size for these mobile devices, and you can build some pretty good logic for a satisfactory user experience. I would NEVER post out to a network source for something as elementary as this.

Oh and on your "mobile site" - if you write your ap semantically well, then you should be able to present a single site for both mobile and browser vs. having to write two separate page sets. Just something to think about - this is worth the extra thought and effort to save time later.

Solution 13 - Browser

I can't see it posted on here, but another option I am looking into currently is www.detectmobilebrowser.com

Solution 14 - Browser

The easiest way is to create an array with regular tags associated with mobile browsers. At least most mobile user agents must have the word mobile, mini, nokia, java ME, android, iphone, mobile OS, etc. If any is matched with the user agent, using php strpos, print a mobile button on top of the page. Leave the user to choose. I love full site cos my mobile browser gives me the same experience, except that I need to zoom or scroll most of the times.

Solution 15 - Browser

You will have to check the user agent string with a previously defined list, like this one

Solution 16 - Browser

you can use a webservice to detect mobile browsing like handsetdetection.com.

Solution 17 - Browser

The fact is that just relying on the useragent is not good enough to detect mobile browsers.

Sure, years ago you could search it for certain strings and guess that it was a Nokia or something, but now there are so many phones out there, and so many that pretend to be things that they are not that something more sophisticated is needed.

I found a great site at link textwhich is based on the same solution that MTV use for all their mobile web sites. It is REALLY good as it has a device independent markup language but more importantly they offer a webservice call for isMobileDevice().

Just look in the manual then 'how it works'.

I've been using it for my customers sites and have yet to find a mobile browser that it doesn't detect accurately. Totally blinding!

Solution 18 - Browser

Just ran across Device and feature detection on the mobile web with these contents:

  1. Using device and feature detection to improve user experience on the mobile web
  2. Introduction to device detection
  3. Approaches to mobile site design
  4. Do nothing
  5. Providing a generic mobile site
  6. Designing with mobile and adaptation in mind
  7. Content adaptation and device grouping strategies
  8. Device grouping
  9. Content adaptation
  10. Minimising the need for adaptation in the first place
  11. Common approaches to device detection
  12. Server side adaptation
  13. Client-side adaptation
  14. Server-side User Agent (UA) and header lookup
  15. Server-side UA string combined with device database lookup
  16. Server-side User Agent Profiles (UAProf) detection
  17. Detection based on JavaScript technology
  18. CSS media types
  19. CSS media queries
  20. Additional best practices
  21. Redirection + manual link
  22. Landing page + manual link
  23. Downloadable sample page

Solution 19 - Browser

you can use WURFL APIs to detect device type

http://wurfl.sourceforge.net/wurfl_schema.php

or Modernizer to detect browser abilities

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeterView Question on Stackoverflow
Solution 1 - BrowserDavidView Answer on Stackoverflow
Solution 2 - BrowserjmccartieView Answer on Stackoverflow
Solution 3 - BrowserAdamView Answer on Stackoverflow
Solution 4 - BrowserVoxPelliView Answer on Stackoverflow
Solution 5 - BrowserPaulView Answer on Stackoverflow
Solution 6 - BrowserMilen A. RadevView Answer on Stackoverflow
Solution 7 - Browserripper234View Answer on Stackoverflow
Solution 8 - BrowserianView Answer on Stackoverflow
Solution 9 - BrowserKlintView Answer on Stackoverflow
Solution 10 - BrowserDave DuPlantisView Answer on Stackoverflow
Solution 11 - BrowserVijesh VPView Answer on Stackoverflow
Solution 12 - BrowserSamuel FullmanView Answer on Stackoverflow
Solution 13 - Browserrrrr-oView Answer on Stackoverflow
Solution 14 - Browser9jabooksView Answer on Stackoverflow
Solution 15 - BrowserhayalciView Answer on Stackoverflow
Solution 16 - BrowserdonView Answer on Stackoverflow
Solution 17 - BrowserRich HoldsworthView Answer on Stackoverflow
Solution 18 - BrowserKevin HakansonView Answer on Stackoverflow
Solution 19 - BrowserPrathamesh RasamView Answer on Stackoverflow