我们通过http的user-agent头判断客户端浏览器类型,下面做个测试
<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>
用各个浏览器访问的结果如下:
一,电脑端
IE8
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
IE9
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Firefox 16
Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0
Safari 5
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.52.7 (KHTML, like Gecko) Version/5.1.2 Safari/534.52.7
Chrome 23
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
Opera 12
Opera/9.80 (Windows NT 6.1; U; Edition Next; zh-cn) Presto/2.10.289 Version/12.01
遨游3
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.12 (KHTML, like Gecko) Maxthon/3.0 Chrome/18.0.966.0 Safari/535.12
(360浏览器,搜狗浏览器等也是基于上面各个浏览器的内核)
二,手机端
海豚浏览器(android)
Mozilla/5.0 (Linux; U; Android 4.0.3; zh-cn; HUAWEI U8818 Build/HuaweiU8818) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
海豚浏览器(iphone)
Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176
傲游浏览器(iphone)
Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3
QQ浏览器(android)
MQQBrowser/3.6/Adr (Linux; U; 4.0.3; zh-cn; HUAWEI U8818 Build/U8818V100R001C17B926;480*800)
QQ浏览器(iphone)
MQQBrowser/371 Mozilla/5.0 (iPhone 4; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 Safari/7534.48.3
魅族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
华为手机浏览器(android)
Mozilla/5.0 (Linux; U; Android 4.0.3; zh-cn; HUAWEI U8818 Build/HuaweiU8818) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Safari(iphone)
Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3
360浏览器(iphone)
Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3
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
chrome浏览器(iphone)
Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X; zh-cn) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/21.0.1180.82 Mobile/9B176 Safari/7534.48.3
可以通过下面的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 ;
}
}
%>