Home >Backend Development >PHP Tutorial >Use PHP and JavaScript to determine whether the request comes from the browser within WeChat, phpjavascript_PHP tutorial

Use PHP and JavaScript to determine whether the request comes from the browser within WeChat, phpjavascript_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:44:161030browse

Use PHP and JavaScript to determine whether the request comes from the browser within WeChat, phpjavascript

HTTP_USER_AGENT of WeChat browser

On iPhone, return

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)

Under Android, return

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)

It is not difficult to find that the WeChat browser is MicroMessenger and has a version number. You can also determine whether the phone type is iPhone or Android

If you want to do hotlinking

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 "请使用微信访问本网址。";
} 


js judgment
function is_weixin(){
  var ua = navigator.userAgent.toLowerCase();
  if(ua.match(/MicroMessenger/i)=="micromessenger") {
    return true;
   } else {
    return false;
  }
}

After identifying the WeChat browser on the mobile phone, you can use WeChat’s sharing js script to process it. You can refer to WeChat’s official documentation. Here is a case to understand the general process and ideas

<script type="text/javascript" src="/jslib/wx_share.js"></script>
    <script>
      //分享链接时给其重写分享的标题、缩略图、链接、简介等
      var imgUrl = '<&#63;php echo base_url('/images/per.png'); &#63;>';
      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>    

 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1049143.htmlTechArticleUse PHP and JavaScript to determine whether the request comes from the browser within WeChat, phpjavascript HTTP_USER_AGENT of WeChat browser on iPhone, return Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn