Home  >  Article  >  Backend Development  >  How to determine whether the page is open on WeChat in PHP

How to determine whether the page is open on WeChat in PHP

WBOY
WBOYOriginal
2016-07-25 09:12:002133browse

There are the following questions: Let a page be opened on WeChat, but cannot be opened directly on the PC.

Example, php determines whether the page is opened by WeChat.

  1. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  2. if (strpos($user_agent, 'MicroMessenger') === false) {
  3. // Non-WeChat browsers are prohibited from browsing
  4. echo "HTTP/1.1 401 Unauthorized";
  5. } else {
  6. // WeChat browser, allow access
  7. echo "MicroMessenger";
  8. // Get the version number
  9. preg_match('/.*?(MicroMessenger/([0-9 .]+))s*/', $user_agent, $matches);
  10. echo '
    Version:'.$matches[2];
  11. }
Copy code

The following are Android, WinPhone , HTTP_USER_AGENT information for iPhone.

  1. "HTTP_USER_AGENT": "Mozilla/5.0 (Linux; U; Android 4.1; zh-cn; Galaxy Nexus Build/Wind-Galaxy Nexus-V1.2) AppleWebKit/534.30 (KHTML, like Gecko) Version/ 4.0 Mobile Safari/534.30 MicroMessenger/5.0.1.352",
  2. "HTTP_USER_AGENT": "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Nokia 920T)",
  3. "HTTP_USER_AGENT": "Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS It can be seen that WinPhone does not have MicroMessenger at all, so it cannot be judged. In fact, the userAgent judgment is very fake, so I think we should use js to judge whether WeixinJSBridge exists, and then use ajax to php PHP detects it and returns the real page information, adding "Loading..." during the process. This is very harmonious. Of course, ajax can also be deceived, but compared to userAgent deception, it is a bit more troublesome. However, our purpose is to detect whether it is the WeChat side, not to deceive. I will not write the specific code, because I hate the kind of borrowing and unlimited copy and paste, and sometimes part of the code is filtered. No matter,

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