微信浏覽器的HTTP_USER_AGENT
在iPhone下,返回
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)
在Android下,返回
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)
不難發現微信浏覽器為 MicroMessenger ,並且有版本號,也可以判斷手機類型為iPhone還是Android
如果要做盜鏈
if(strpos($_SERVER["HTTP_USER_AGENT"],"MicroMessenger")) echo "Welcome to wechat word"; else echo "http/1.1 401 Unauthorized"; public function is_weixin(){ if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) { return true; } return false; } if($this->is_weixin()){ }else{ echo "請使用微信訪問本網址。"; }
function is_weixin(){ var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMessenger/i)=="micromessenger") { return true; } else { return false; } }
當在手機端判斷到微信浏覽器之後,可以使用微信的分享js腳本對其進行處理,大家可以參考微信的官方文檔,這裡提供一個案例,了解下大概流程和思路
<script type="text/javascript" src="/jslib/wx_share.js"></script> <script> //分享鏈接時給其重寫分享的標題、縮略圖、鏈接、簡介等 var imgUrl = '<?php echo base_url('/images/per.png'); ?>'; var lineLink = 'http://www.baidu.com/'; var shareTitle = '我來給你送錢了'; var descContent = '折射率,你知道是啥?'; var timeline_title = 'timeline_title'; var appid = ''; </script> <script> function onBridgeReady() { WeixinJSBridge.call('showOptionMenu'); } </script> <script> if (typeof WeixinJSBridge == "undefined") { if (document.addEventListener) { document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false); } else if (document.attachEvent) { document.attachEvent('WeixinJSBridgeReady', onBridgeReady); document.attachEvent('onWeixinJSBridgeReady', onBridgeReady); } } else { onBridgeReady(); } </script> </head>