赞
踩
首先收集了部分客户端请求头部信息如下
iPhone微信:
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B440 MicroMessenger/6.2.4 NetType/WIFI Language/en
iPhone—safari
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B440 Safari/600.1.4
Android-Chrome
User-Agent:Mozilla/5.0 (Linux; Android 4.4.4; HTC D820u Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.89 Mobile Safari/537.36
PC-Chrome
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36
PC-Firefox
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
PC-IE
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
我们需要判断Http请求是来自手机端还是电脑端,关键是取得User-Agent的信息,进行筛选判断即可。
- public static boolean isMobileDevice(String requestHeader){
- /**
- * android : 所有android设备
- * mac os : iphone ipad
- * windows phone:Nokia等windows系统的手机
- */
- String[] deviceArray = new String[]{"android","mac os","windows phone"};
- if(requestHeader == null)
- return false;
- requestHeader = requestHeader.toLowerCase();
- for(int i=0;i<deviceArray.length;i++){
- if(requestHeader.indexOf(deviceArray[i])>0){
- return true;
- }
- }
- return false;
- }
- String requestHeader = request.getHeader("user-agent");
- if(JudgeRequestDeviceUtil.isMobileDevice(requestHeader)){
- logger.debug("使用手机浏览器");
- }else{
- logger.debug("使用web浏览器");
- }
-
- function is_in_wx(){
- $user_agent = $_SERVER['HTTP_USER_AGENT'];
- return false!=strpos($user_agent, 'MicroMessenger');
- }
-
- function is_mobile_visit()
- {
- // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
- if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
- {
- return true;
- }
- // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
- if (isset ($_SERVER['HTTP_VIA']))
- {
- // 找不到为flase,否则为true
- return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
- }
- // 脑残法,判断手机发送的客户端标志,兼容性有待提高
- if (isset ($_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',
- 'openwave',
- 'nexusone',
- 'cldc',
- 'midp',
- 'wap',
- 'mobile'
- );
- // 从HTTP_USER_AGENT中查找手机浏览器的关键字
- if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
- {
- return true;
- }
- }
- // 协议法,因为有可能不准确,放到最后判断
- if (isset ($_SERVER['HTTP_ACCEPT']))
- {
- // 如果只支持wml并且不支持html那一定是移动设备
- // 如果支持wml和html但是wml在html之前则是移动设备
- if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
- {
- return true;
- }
- }
- return false;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。