Example of php determining whether the page is opened by WeChat_PHP tutorial

WBOY
Release: 2016-07-13 10:31:47
Original
903 people have browsed it

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 the data.

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

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 and then returns the real page information, adding a "Loading..." during the period. This is very harmonious. Of course, ajax can also be deceived, but compared to userAgent deception, it is a little more troublesome. Yes,

However, our purpose is to detect whether it is WeChat, not to deceive. I will not write the specific code, because I hate the kind of use-ism, unlimited copying and pasting, and sometimes the code is filtered. I don’t care about a part of it,

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/761298.htmlTechArticleThe 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; }...
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