Home > Backend Development > PHP Problem > How to check if php is the WeChat client

How to check if php is the WeChat client

藏色散人
Release: 2023-03-14 13:26:01
Original
4159 people have browsed it

php method to check whether it is WeChat: 1. Open the corresponding PHP code file; 2. Pass "if (strpos($user_agent, 'MicroMessenger') === false) {...}else{ ...}" statement can be used to make the judgment.

How to check if php is the WeChat client

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

How to check if php is the WeChat client?

php determines whether it is the browser access of the WeChat client

Code:

$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($user_agent, 'MicroMessenger') === false) {
    // 非微信浏览器禁止浏览
    echo "HTTP/1.1 401 Unauthorized";
} else {
    // 微信浏览器,允许访问
    echo "MicroMessenger";
    // 获取版本号
    preg_match('/.*?(MicroMessenger\/([0-9.]+))\s*/', $user_agent, $matches);
    echo &#39;<br>Version:&#39;.$matches[2];
}
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to check if php is the WeChat client. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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