Want to show “mobile” or “phone” site version — check user agent
So, you (i, we) want to show to our site phone visitor “mobile” (intended for phone users) version of the site.
First of all we must detect “mobile” request. To do this we must check user agent header from request.
in Java (withing relevant servlet method ):
String userAgent = request.getHeader("user-agent");
in PHP:
$user_agent = $_SERVER['HTTP_USER_AGENT'];
in JavaScript (in this case its not really from request but directly from browser)
var userAgent = navigator.userAgent;
in GWT (the same note as above)
String userAgent = Window.Navigator.getUserAgent();
The next step is to search in user agent string for some mobile specific substring. Lets call it MOBILE_SPECIFIC_SUBSTRING. What values can be treated as mobile specific?
1. “iPhone” – all iPhone models contain string “iPhone” in their user agent properties.
2. “Android” – same note as above with some exceptions.
3. “MIDP” – many mobile devices contain string “MIDP”. Some of this devices can have screen big enough to properly display regular version of your site. So think..
4. “Opera Mobi” – some of mobiles contain this string detecting mobile version of Opera.
5. “Opera Mini” – some of mobiles contain this string …
6. “BlackBerry” – one of most popular smartphones.
7. “HP iPAQ” – Hewlett Packard smartphone?
8. “IEMobile” – microsoft IE mobile version.
9. “MSIEMobile” – the same as above.
10.”Windows Phone” – microsoft mobile OS detection.
11.”HTC” – sometimes htc phones contain nothing specific but this.
12.”LG” – LG.
13.”MOT” – Motorola.
14.”Nokia” – just Nokia.
15.”Symbian” – Symbian OS detection.
16.”Fennec” – mobile Firefox.
17.”Maemo” – linux version from Nokia. Is it may be used only on phones?
18.”Tear” – some mobile browser from/for Nokia?
19.”Midori” – it seems like Nokia gays have nothing to do. For what so many browsers?
20.”armv6l” – some string detecting Nokia phones.
21.”armv7l” – .. above. “armv” can be used as generic version.
22.”Windows CE”.
23.”WindowsCE” – variant of above.
24.”Smartphone” – some smartphones (why not all?!) contain this string.
25.”240×320″ – .. above. I see the future: soon (already?) web designers will take these parameters directly to their site width and height.
26.”176×220″, “320×320″, “160×160″ – same as above.
27.”webOS” – Palm webOS detection.
28.”Palm” – just Palm.
29.”Sagem” – more less known phone.
30.”Samsung”.
31.”SGH” – Samsung.
32.”Siemens”, “SIE” – Siemens.
33.”SonyEricsson”.
34.”MMP” some phones contain this string in their user-agents.
35.”UCWEB” – just another mobile browser.
That’s may be not all, but i think that is enough. (First time in my life i typed word “inaf” without mistakes
).
I take all values from this great research.
The code below is for GWT
static final String[] MOBILE_SPECIFIC_SUBSTRING = {
"iPhone","Android","MIDP","Opera Mobi",
"Opera Mini","BlackBerry","HP iPAQ","IEMobile",
"MSIEMobile","Windows Phone","HTC","LG",
"MOT","Nokia","Symbian","Fennec",
"Maemo","Tear","Midori","armv",
"Windows CE","WindowsCE","Smartphone","240x320",
"176x220","320x320","160x160","webOS",
"Palm","Sagem","Samsung","SGH",
"SIE","SonyEricsson","MMP","UCWEB"};
...
if (checkMobile()){
buildMobileSiteView();
} else {
buildRegularSiteView();
}
...
private boolean checkMobile() {
String userAgent = Window.Navigator.getUserAgent();
for (String mobile: MOBILE_SPECIFIC_SUBSTRING){
if (userAgent.contains(mobile)
|| userAgent.contains(mobile.toUpperCase())
|| userAgent.contains(mobile.toLowerCase())){
return true;
}
}
return false;
}
Above code will caught 99% of all mobile devices. imho. o_~
None of known browsers or bots contains above strings. No warranties of any kind.
Related posts:
- How to check that site built using WordPress
- MySQL on Windows Access denied for user ‘root’@'localhost’ (using password: NO)
- The plugin does not exist or no valid version could be found























