Example of PHP determining whether the page is opened by WeChat (opening the webpage by WeChat)_PHP tutorial

WBOY
Release: 2016-07-13 10:31:49
Original
727 people have browsed it

Copy code The code is as follows:

$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 version number
preg_match('/.*?(MicroMessenger/([0-9.]+))s*/', $user_agent, $matches);
echo '
Version:'.$matches[2];
}

Well, I can’t refute it directly, but this is indeed very unreliable.
Let’s talk about it with data.
The following are the HTTP_USER_AGENT information for Android, WinPhone, and iPhone.

Copy code The code is as follows:

"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 X ) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10B329 MicroMessenger/5.0.1",

You can see that WinPhone does not have MicroMessenger at all, so it is impossible to judge. 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 one Page information, add a "Loading..." during the period, it will be very harmonious. Of course, ajax can also be deceived, but compared to userAgent deception, it is a little more troublesome.
But our purpose is I will not write the specific code to detect whether it is the WeChat side, not a deception issue, because I hate the kind of arbitrary copying and pasting the most, and sometimes the code is filtered and part of it is ignored.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/761017.htmlTechArticleCopy the code as follows: $user_agent = $_SERVER['HTTP_USER_AGENT']; if (strpos($user_agent, ' MicroMessenger') === false) { // Non-WeChat browsers prohibit browsing echo "HTTP/1.1 401 Un...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!