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.
-
- $user_agent = $_SERVER['HTTP_USER_AGENT'];
- if (strpos($user_agent, 'MicroMessenger') === false) {
- // Non-WeChat browsers are prohibited from browsing
- echo "HTTP/1.1 401 Unauthorized";
- } else {
- // WeChat browser, allow access
- echo "MicroMessenger";
- // Get the version number
- preg_match('/.*?(MicroMessenger/([0-9 .]+))s*/', $user_agent, $matches);
- echo '
Version:'.$matches[2];
- }
Copy code
The following are Android, WinPhone , HTTP_USER_AGENT information for iPhone.
-
- "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",
- "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)",
- "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,
|