Home > PHP Framework > YII > yii2 determines whether it comes from WeChat browser

yii2 determines whether it comes from WeChat browser

angryTom
Release: 2020-02-17 17:42:28
Original
2638 people have browsed it

This article mainly introduces the use of yii2 to determine whether the request comes from the browser in WeChat. Friends in need can refer to it.

yii2 determines whether it comes from WeChat browser

yii2 determines whether it comes from WeChat browser

under iPhone, return

Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 MicroMessenger/4.3.2)
Copy after login

under Android, Return

Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255)
Copy after login

It is not difficult to find that the WeChat browser is MicroMessenger and has a version number. You can also determine whether the phone type is iPhone or Android

The following is a method of using native php to determine:

public function is_weixin(){ 
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
        return true;
    }
    return false;
}    
if($this->is_weixin()){
    // TODO
}else{
    echo "请使用微信访问本网址。";
}
Copy after login

is implemented using YII. The yii request component provides a method to support quick access to common headers:

Yii::$app->request->userAgent; //返回 User-Agent 头。
Copy after login
public function is_weixin(){ 
    if ( strpos(Yii::$app->request->userAgent, 'MicroMessenger') !== false ) {
        return true;
    }
    return false;
}    
if($this->is_weixin()){
    // TODO
}else{
    echo "请使用微信访问本网址。";
}
Copy after login

Recommended related articles and tutorials: yii tutorial

The above is the detailed content of yii2 determines whether it comes from WeChat browser. For more information, please follow other related articles on the PHP Chinese website!

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