PHP使用$_SERVER['HTTP_USER_AGENT'];
获取User Agent信息,通过查找信息的异同并加以判断语句来实现只允许页面在微信内置浏览器打开,或只允许在手机QQ内置浏览器打开,或其它浏览器打开等。
安卓系统
手机UC浏览器:
Mozilla/5.0 (Linux; U; Android 4.2.2; zh-CN; Hol-T00 Build/HUAWEIHol-T00) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 UCBrowser/10.7.5.655 U3/0.8.0 Mobile Safari/534.30
手机QQ浏览器:
Mozilla/5.0 (Linux; U; Android 4.2.2; zh-cn; Hol-T00 Build/HUAWEIHol-T00) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 MQQBrowser/6.1 Mobile Safari/537.36
手机QQ内置浏览器:
Mozilla/5.0 (Linux; U; Android 4.2.2; zh-cn; Hol-T00 Build/HUAWEIHol-T00) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.4 TBS/025477 Mobile Safari/533.1 V1_AND_SQ_5.9.1_272_YYB_D QQ/5.9.1.2535 NetType/WIFI WebP/0.3.0 Pixel/720
微信内置浏览器:
Mozilla/5.0 (Linux; U; Android 4.2.2; zh-cn; Hol-T00 Build/HUAWEIHol-T00) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.4 TBS/025478 Mobile Safari/533.1 MicroMessenger/6.3.5.50_r1573191.640 NetType/WIFI Language/zh_CN
手机百度:
Mozilla/5.0 (Linux; U; Android 4.2.2; zh-cn; Hol-T00 Build/HUAWEIHol-T00) AppleWebKit/534.24 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.24 T5/2.0 baiduboxapp/6.9.1 (Baidu; P1 4.2.2)
IOS系统
微信内置浏览器:(iPhone 4S)
Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13B143 MicroMessenger/6.3.6 NetType/WIFI Language/zh_CN
手机QQ内置浏览器:(iPhone 4S)
Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13B143 MicroMessenger/6.3.6 NetType/WIFI Language/zh_CN
手机QQ内置浏览器:(iPhone plus)
Mozilla/5.0 (iPhone; CPU iPhone OS 8_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12D508 QQ/5.9.1.405 Pixel/1080 NetType/WIFI Mem/132
手机QQ内置浏览器:(iPhone plus)
Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13B143 QQ/5.9.1.405 Pixel/1080 NetType/2G Mem/208
JavaScript和PHP获取后判断信息方法如下:
通过 JavaScript 函数判断
0 1 2 3 4 5 6 7 8 9 |
<script> function is_weixin(){ var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMessenger/i)=="micromessenger") { return true; } else { return false; } } </script> |
通过 PHP 函数判断
0 1 2 3 4 5 6 7 8 |
<?php function is_weixin(){ //stripos(不区分大小写),strpos(区分大小写) if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) { return true; } return false; } ?> |
实例:通过 PHP 判断只允许在微信内置浏览器打开
0 1 2 3 4 5 6 7 |
<?php $weixin = $_SERVER['HTTP_USER_AGENT']; $weixin = stripos($weixin,"MicroMessenger"); if($weixin == null){ echo "请使用微信客户端打开!"; exit; } ?> |