我们可以通过userAgent来判断,比如检测某些关键字,例如:AppleWebKit*****Mobile或AppleWebKit,需要注意的是有些浏览器的userAgent中并不包含AppleWebKit关键字,可能会是Linux,UCBrowser等等。
1. Javascript
2. PHP
3. .NET (C#)
<script type="text/javascript">
/*
* 智能机浏览器版本信息:
*/
var browser={
versions:function(){
var u = navigator.userAgent, app = navigator.appVersion;
return {//移动终端浏览器版本信息
trident: u.indexOf('Trident') > -1, //IE内核
presto: u.indexOf('Presto') > -1, //opera内核
webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
mobile: !!u.match(/AppleWebKit.*Mobile.*/)||!!u.match(/AppleWebKit/), //是否为移动终端
ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQ HD浏览器
iPad: u.indexOf('iPad') > -1, //是否iPad
webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部
};
}(),
language:(navigator.browserLanguage || navigator.language).toLowerCase()
}
document.writeln("语言版本: "+browser.language);
document.writeln(" 是否为移动终端: "+browser.versions.mobile);
document.writeln(" ios终端: "+browser.versions.ios);
document.writeln(" android终端: "+browser.versions.android);
document.writeln(" 是否为iPhone: "+browser.versions.iPhone);
document.writeln(" 是否iPad: "+browser.versions.iPad);
document.writeln(navigator.userAgent);
</script>
<?php
/**
* 判断是否是通过手机访问
* @return bool 是否是移动设备
*/
public function isMobile() {
//判断手机发送的客户端标志
if(isset($_SERVER['HTTP_USER_AGENT'])) {
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
$clientkeywords = array(
'nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-'
,'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu',
'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini',
'operamobi', 'opera mobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile'
);
// 从HTTP_USER_AGENT中查找手机浏览器的关键字
if(preg_match("/(".implode('|',$clientkeywords).")/i",$userAgent)&&strpos($userAgent,'ipad') === false)
{
return true;
}
}
return false;
}
下面的jsp代码简单判断浏览器类型,如果是iphone或者android就跳转到手机版本网页:
<%
String ua = request.getHeader("User-Agent") ;
if (ua != null) {
if (ua.indexOf("iPhone") >-1 || ua.indexOf("iPad") >-1 || (ua.indexOf("ndroid") >-1 && ua.indexOf("WebKit") >-1)) {
response.sendRedirect("http://localhost/mobile");
return ;
}
}
%>
下面是.NET代码(C#)
在.NET下,其中一个方法是利用HttpBrowserCapabilities,代码如下:
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = "Browser Capabilities\n"
+ "Type = " + browser.Type + "\n"
+ "Name = " + browser.Browser + "\n"
+ "Version = " + browser.Version + "\n"
+ "Major Version = " + browser.MajorVersion + "\n"
+ "Minor Version = " + browser.MinorVersion + "\n"
+ "Platform = " + browser.Platform + "\n"
+ "Is Beta = " + browser.Beta + "\n"
+ "Is Crawler = " + browser.Crawler + "\n"
+ "Is AOL = " + browser.AOL + "\n"
+ "Is Win16 = " + browser.Win16 + "\n"
+ "Is Win32 = " + browser.Win32 + "\n"
+ "Supports Frames = " + browser.Frames + "\n"
+ "Supports Tables = " + browser.Tables + "\n"
+ "Supports Cookies = " + browser.Cookies + "\n"
+ "Supports VBScript = " + browser.VBScript + "\n"
+ "Supports JavaScript = " +
browser.EcmaScriptVersion.ToString() + "\n"
+ "Supports Java Applets = " + browser.JavaApplets + "\n"
+ "Supports ActiveX Controls = " + browser.ActiveXControls
+ "\n"
+ "Supports JavaScript Version = " +
browser["JavaScriptVersion"] + "\n";
在web.config中需要在filter中进行匹配
<system.web> <browserCaps> <use var="HTTP_USER_AGENT" />
<filter> <case match="Windows NT 6.1"> platform=Windows7 </case> </filter>
</browserCaps> </system.web>
另外一个方法:
///<summary>
/// 根据 Agent 判断是否是智能手机
///</summary>
///<returns></returns>
public static bool CheckAgent()
{
bool flag = false;
string agent = HttpContext.Current.Request.UserAgent;
string[] keywords = { "Android", "iPhone", "iPod", "iPad", "Windows Phone", "MQQBrowser" };
//排除 Windows 桌面系统
if (!agent.Contains("Windows NT") || (agent.Contains("Windows NT") && agent.Contains("compatible; MSIE 9.0;")))
{
//排除 苹果桌面系统
if (!agent.Contains("Windows NT") && !agent.Contains("Macintosh"))
{
foreach (string item in keywords)
{
if (agent.Contains(item))
{
flag = true;
break;
}
}
}
}
return flag;
}
代码解释:
1. !agent.Contains("Windows NT") && !agent.Contains("Macintosh") 排除Window 桌面系统 和 苹果桌面系统
2. "Android", "iPhone", "iPod", "iPad", "Windows Phone", "MQQBrowser" 这些是判断智能设备Agent中的关键词
3. MQQBrowser 为 QQ 手机浏览器,QQ 手机的Agent 比较特殊,所以单独判断
各移动浏览器的http user-agent:
一、微信的http user-agent(关键词MicroMessenger)
1. Iphone(苹果)平台微信的ucweb的useragent:
Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 MicroMessenger/4.3.2
2. Android(安卓)平台微信的ucweb的useragent:1
Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255
二、UC浏览器的http user-agent(关键词UCWEB)
1. android平台:
android平台ucweb急速模式开启下ucweb浏览器useragent:
UCWEB/2.0 (Linux; U; Adr 2.3; zh-CN; MI-ONEPlus) U2/1.0.0 UCBrowser/8.6.0.199 U2/1.0.0 Mobile
android平台ucweb急速模式关闭状态下的http_user_agent:
Mozilla/5.0 (Linux; U; Android 2.3; zh-CN; MI-ONEPlus) AppleWebKit/534.13 (KHTML, like Gecko) UCBrowser/8.6.0.199 U3/0.8.0 Mobile Safari/534.13
2. iPhone 平台
iPhone 平台极速模式开启状态下ucweb浏览器useragent:
UCWEB/2.0 (iOS; U; iPh OS 4_3_2; zh-CN; iPh4) U2/1.0.0 UCBrowser/8.6.0.199 U2/1.0.0 Mobile
iPhone 平台极速模式关闭状态下 UA 示例如下:(OBUA 为自带浏览器 UA)\
OBUA UCBrowser/8.6.0.199 Mobile
3. iPad 平台
ipad平台下ucweb浏览器useragent:
Mozilla/5.0 (iPad; U; CPU OS 6_0 like Mac OS X; zh-CN; iPad2) AppleWebKit/534.13 (KHTML, like Gecko) UCBrowser/8.6.0.199 U3/0.8.0 Safari/534.13
4. windows Phone平台
WP平台下ucweb浏览器useragent:
WP平台极速模式开启状态下:(以 Nokia 900 为例)
UCWEB/2.0 (Windows; U; wds7.10; zh-CN; Nokia 900) U2/1.0.0 UCBrowser/8.6.0.199 U2/1.0.0 Mobile
5. 诺基亚平台
诺基亚手机Symbian&Java平台ucweb浏览器useragen:
Nokia 5800 XpressMusic/UCWEB8.9.0.253/50/999
安卓QQ浏览器HD版检测的结果是:mac, Safari,这个很是变态,自己看着处理吧
3个检测浏览器User-Agent信息的网站
三、一些特别的浏览器
QQ浏览器(android)
MQQBrowser/3.6/Adr (Linux; U; 4.0.3; zh-cn; HUAWEI U8818 Build/U8818V100R001C17B926;480*800)
魅族UC浏览器(android)
JUC (Linux; U; 2.3.5; zh-cn; MEIZU MX; 640*960) UCWEB8.5.1.179/145/33232
UC浏览器(iphone)
IUC(U;iOS 5.1;Zh-cn;320*480;)/UCWEB8.8.0.212/42/997
Opera mobile(android)
Opera/9.80 (Android 4.0.3; Linux; Opera Mobi/ADR-1210241554) Presto/2.11.355 Version/12.10
Opera mini(iphone)
Opera/9.80 (iPhone; Opera Mini/7.0.5/28.2690; U; zh) Presto/2.8.119 Version/11.10
三个在线网站,通过手机浏览器就可以在线检测
1. http://whatsmyuseragent.com/
2. http://whatsmyua.com/
3. http://www.useragentstring.com/