Table of Contents
错误信息:
Home CMS Tutorial PHPCMS How to implement WeChat login in phpcms

How to implement WeChat login in phpcms

Mar 09, 2023 am 09:33 AM
WeChat phpcms

phpcms method to implement Wechat login: 1. Create a new "wechat.php" in the root directory; 2. Add "public function wechat() {. ..}"; 3. Use the wechat function in the "foreground.class.php" file to determine whether the user is logged in.

How to implement WeChat login in phpcms

The operating environment of this tutorial: Windows 10 system, phpcms v9 version, DELL G3 computer

How to implement WeChat login in phpcms?

phpcms implements WeChat login (no registration required, direct deposit)

Idea: callback parameters, register directly (the code is not standardized enough, organize it yourself)

At the root Create a new directory wechat.php

Visit: http://www.xxxxxx.cn/wechat.php

Note the callback address

phpcms implements WeChat login (no registration required, directly Deposit)

wechat.php

<?php
if(!empty( $_GET[&#39;code&#39;]) && !empty( $_GET[&#39;state&#39;])){
    $code = $_GET[&#39;code&#39;];
    $state = $_GET[&#39;state&#39;];
    $url = "http://www.xxxxxx.cn/index.php?m=member&c=index&a=wechat&code=$code&state=$state";
    header(&#39;location:&#39;.$url);
    exit;
}
$appid = &#39;wx5a3878682fa32bd5&#39;;
$url = "https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=http://www.xxxxxx.cn/wechat.php&response_type=code&scope=snsapi_login&state=1&connect_redirect=1#wechat_redirect";
header(&#39;location:&#39;.$url);
?>
Copy after login

\phpcms\modules\member\index.php and add

Note: The registration status output $status, you can return error information according to Error message modification parameter content

The user name and password are simply distinguished and modified according to the actual situation. The email address is a required field, so just put one in it.

<?php
public function wechat() {
        $this->_session_start();
        //获取用户siteid
        $siteid = isset($_REQUEST[&#39;siteid&#39;]) && trim($_REQUEST[&#39;siteid&#39;]) ? intval($_REQUEST[&#39;siteid&#39;]) : 1;
        //定义站点id常量
        if (!defined(&#39;SITEID&#39;)) {
           define(&#39;SITEID&#39;, $siteid);
        }
        //加载用户模块配置
        //加载用户模块配置
        $member_setting = getcache(&#39;member_setting&#39;);
        if(!$member_setting[&#39;allowregister&#39;]) {
            showmessage(L(&#39;deny_register&#39;), &#39;/login.html&#39;);
        }
        /*----------------------微信获取用户信息------------------------*/
        //换成自己的接口信息
        $code = $_GET[&#39;code&#39;];
        $state = $_GET[&#39;state&#39;];
        $appid = &#39;xxxxxxxx&#39;;
        $appsecret = &#39;xxxxxxxx&#39;;
        if (empty($code)) $this->error(&#39;授权失败&#39;);
        $token_url = &#39;https://api.weixin.qq.com/sns/oauth2/access_token?appid=&#39;.$appid.&#39;&secret=&#39;.$appsecret.&#39;&code=&#39;.$code.&#39;&grant_type=authorization_code&#39;;
        $token = json_decode(file_get_contents($token_url));
        if (isset($token->errcode)) {
            showmessage(L(&#39;<br/><h2 id="错误信息">错误信息:</h2>&#39;.$token->errmsg), HTTP_REFERER);
            exit;
        }
        $access_token_url = &#39;https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=&#39;.$appid.&#39;&grant_type=refresh_token&refresh_token=&#39;.$token->refresh_token;
        //转成对象
        $access_token = json_decode(file_get_contents($access_token_url));
        if (isset($access_token->errcode)) {
            showmessage(L(&#39;<br/><h2 id="错误信息">错误信息:</h2>&#39;.$access_token->errmsg), HTTP_REFERER);
            exit;
        }
        $user_info_url = &#39;https://api.weixin.qq.com/sns/userinfo?access_token=&#39;.$access_token->access_token.&#39;&openid=&#39;.$access_token->openid.&#39;&lang=zh_CN&#39;;
        //转成对象
        $user_info = json_decode(file_get_contents($user_info_url));
        if (isset($user_info->errcode)) {
            showmessage(L( &#39;<br/><h2 id="错误信息">错误信息:</h2>&#39;.$user_info->errmsg), HTTP_REFERER);
            exit;
        }
        $rs =  json_decode(json_encode($user_info),true);//转换成数组
        /*------------------------获取用户信息代码结束-----------------------*/
        header("Cache-control: private");
        $checkname = trim($rs[&#39;nickname&#39;])."_".substr(md5($rs[&#39;unionid&#39;]),0,5) ;
        $this->_init_phpsso();
        $cstatus = $this->client->ps_checkname($checkname);
        //如果存在用户 用户登陆   
        if($cstatus == -4 || $cstatus == -1) {
            $username = $rs[&#39;nickname&#39;]."_".substr(md5($rs[&#39;unionid&#39;]),0,5) ;
            $password = substr(md5($rs[&#39;unionid&#39;]),0,10);
            //is_password($_POST[&#39;password&#39;]) && is_badword($_POST[&#39;password&#39;])==false ? trim($_POST[&#39;password&#39;]) : showmessage(L(&#39;password_format_incorrect&#39;), HTTP_REFERER);
            $cookietime = intval($_POST[&#39;cookietime&#39;]);
            $synloginstr = &#39;&#39;; //同步登陆js代码
            if(pc_base::load_config(&#39;system&#39;, &#39;phpsso&#39;)) {
                $this->_init_phpsso();
                $status = $this->client->ps_member_login($username, $password);
                $memberinfo = unserialize($status);
                if(isset($memberinfo[&#39;uid&#39;])) {
                    //查询帐号
                    $r = $this->db->get_one(array(&#39;phpssouid&#39;=>$memberinfo[&#39;uid&#39;]));
                    if(!$r) {
                        //插入会员详细信息,会员不存在 插入会员
                        $info = array(
                                    &#39;phpssouid&#39;=>$memberinfo[&#39;uid&#39;],
                                    &#39;username&#39;=>$memberinfo[&#39;username&#39;],
                                    &#39;password&#39;=>$memberinfo[&#39;password&#39;],
                                    &#39;encrypt&#39;=>$memberinfo[&#39;random&#39;],
                                    &#39;email&#39;=>$memberinfo[&#39;email&#39;],
                                    &#39;regip&#39;=>$memberinfo[&#39;regip&#39;],
                                    &#39;regdate&#39;=>$memberinfo[&#39;regdate&#39;],
                                    &#39;lastip&#39;=>$memberinfo[&#39;lastip&#39;],
                                    &#39;lastdate&#39;=>$memberinfo[&#39;lastdate&#39;],
                                    &#39;groupid&#39;=>$this->_get_usergroup_bypoint(),    //会员默认组
                                    &#39;modelid&#39;=>10,  //普通会员
                                    );
                        //如果是connect用户
                        if(!empty($_SESSION[&#39;connectid&#39;])) {
                            $userinfo[&#39;connectid&#39;] = $_SESSION[&#39;connectid&#39;];
                        }
                        if(!empty($_SESSION[&#39;from&#39;])) {
                            $userinfo[&#39;from&#39;] = $_SESSION[&#39;from&#39;];
                        }
                        unset($_SESSION[&#39;connectid&#39;], $_SESSION[&#39;from&#39;]);
                        $this->db->insert($info);
                        unset($info);
                        $r = $this->db->get_one(array(&#39;phpssouid&#39;=>$memberinfo[&#39;uid&#39;]));
                    }
                    $password = $r[&#39;password&#39;];
                    $synloginstr = $this->client->ps_member_synlogin($r[&#39;phpssouid&#39;]);
                } else {
                    if($status == -1) {    //用户不存在
                        showmessage(L(&#39;user_not_exist&#39;), &#39;/login.html&#39;);
                    } elseif($status == -2) { //密码错误
                        showmessage(L(&#39;password_error&#39;), &#39;/login.html&#39;);
                    } else {
                        showmessage(L(&#39;login_failure&#39;), &#39;/login.html&#39;);
                    }
                }
            } else {
                //密码错误剩余重试次数
                $this->times_db = pc_base::load_model(&#39;times_model&#39;);
                $rtime = $this->times_db->get_one(array(&#39;username&#39;=>$username));
                if($rtime[&#39;times&#39;] > 4) {
                    $minute = 60 - floor((SYS_TIME - $rtime[&#39;logintime&#39;]) / 60);
                    showmessage(L(&#39;wait_1_hour&#39;, array(&#39;minute&#39;=>$minute)));
                }
                //查询帐号
                $r = $this->db->get_one(array(&#39;username&#39;=>$username));
                if(!$r) showmessage(L(&#39;user_not_exist&#39;),&#39;/login.html&#39;);
                //验证用户密码
                $password = md5(md5(trim($password)).$r[&#39;encrypt&#39;]);
                if($r[&#39;password&#39;] != $password) {                
                    $ip = ip();
                    if($rtime && $rtime[&#39;times&#39;] < 5) {
                        $times = 5 - intval($rtime[&#39;times&#39;]);
                        $this->times_db->update(array(&#39;ip&#39;=>$ip, &#39;times&#39;=>&#39;+=1&#39;), array(&#39;username&#39;=>$username));
                    } else {
                        $this->times_db->insert(array(&#39;username&#39;=>$username, &#39;ip&#39;=>$ip, &#39;logintime&#39;=>SYS_TIME, &#39;times&#39;=>1));
                        $times = 5;
                    }
                    showmessage(L(&#39;password_error&#39;, array(&#39;times&#39;=>$times)), &#39;/login.html&#39;, 3000);
                }
                $this->times_db->delete(array(&#39;username&#39;=>$username));
            }
            //如果用户被锁定
            if($r[&#39;islock&#39;]) {
                showmessage(L(&#39;user_is_lock&#39;));
            }
            $userid = $r[&#39;userid&#39;];
            $groupid = $r[&#39;groupid&#39;];
            $username = $r[&#39;username&#39;];
            $nickname = empty($r[&#39;nickname&#39;]) ? $username : $r[&#39;nickname&#39;];
            $updatearr = array(&#39;lastip&#39;=>ip(), &#39;lastdate&#39;=>SYS_TIME);
            //vip过期,更新vip和会员组
            if($r[&#39;overduedate&#39;] < SYS_TIME) {
                $updatearr[&#39;vip&#39;] = 0;
            }       
            //检查用户积分,更新新用户组,除去邮箱认证、禁止访问、游客组用户、vip用户,如果该用户组不允许自助升级则不进行该操作        
            if($r[&#39;point&#39;] >= 0 && !in_array($r[&#39;groupid&#39;], array(&#39;1&#39;, &#39;7&#39;, &#39;8&#39;)) && empty($r[vip])) {
                $grouplist = getcache(&#39;grouplist&#39;);
                if(!empty($grouplist[$r[&#39;groupid&#39;]][&#39;allowupgrade&#39;])) {   
                    $check_groupid = $this->_get_usergroup_bypoint($r[&#39;point&#39;]);
                    if($check_groupid != $r[&#39;groupid&#39;]) {
                        $updatearr[&#39;groupid&#39;] = $groupid = $check_groupid;
                    }
                }
            }
            //如果是connect用户
            if(!empty($_SESSION[&#39;connectid&#39;])) {
                $updatearr[&#39;connectid&#39;] = $_SESSION[&#39;connectid&#39;];
            }
            if(!empty($_SESSION[&#39;from&#39;])) {
                $updatearr[&#39;from&#39;] = $_SESSION[&#39;from&#39;];
            }
            unset($_SESSION[&#39;connectid&#39;], $_SESSION[&#39;from&#39;]);
            $this->db->update($updatearr, array(&#39;userid&#39;=>$userid));
            if(!isset($cookietime)) {
                $get_cookietime = param::get_cookie(&#39;cookietime&#39;);
            }
            $_cookietime = $cookietime ? intval($cookietime) : ($get_cookietime ? $get_cookietime : 0);
            $cookietime = $_cookietime ? SYS_TIME + $_cookietime : 0;
            $phpcms_auth = sys_auth($userid."\t".$password, &#39;ENCODE&#39;, get_auth_key(&#39;login&#39;));
            param::set_cookie(&#39;auth&#39;, $phpcms_auth, $cookietime);
            param::set_cookie(&#39;_userid&#39;, $userid, $cookietime);
            param::set_cookie(&#39;_username&#39;, $username, $cookietime);
            param::set_cookie(&#39;_groupid&#39;, $groupid, $cookietime);
            param::set_cookie(&#39;_nickname&#39;, $nickname, $cookietime);
            //print_r($_COOKIE);
            //exit;
            //param::set_cookie(&#39;cookietime&#39;, $_cookietime, $cookietime); 
            //https 与 http 传递用户名   跨域跳转cooike
            $forward = isset($_POST[&#39;forward&#39;]) && !empty($_POST[&#39;forward&#39;]) ? urldecode($_POST[&#39;forward&#39;]) : &#39;index.php?m=member&c=index&#39;;
            $url = "http://www.zhiliaoke.com.cn/set_cookie_www.php?nickname=".$_COOKIE[&#39;vuGYB__nickname&#39;]."&username=".$_COOKIE[&#39;vuGYB__username&#39;]."&userid=".$_COOKIE[&#39;vuGYB__userid&#39;]."&groupid=".$_COOKIE[&#39;vuGYB__groupid&#39;];
            header("Location: ".$url.""); 
        } 
        //如果不存在用户注册
            $userinfo = array();
            $userinfo[&#39;encrypt&#39;] = create_randomstr(6);
            $userinfo[&#39;username&#39;] = $rs[&#39;nickname&#39;]."_".substr(md5($rs[&#39;unionid&#39;]),0,5) ;
            $userinfo[&#39;nickname&#39;] = $rs[&#39;nickname&#39;]."_".substr(md5($rs[&#39;unionid&#39;]),0,5) ;
            $userinfo[&#39;email&#39;] = time().&#39;@yangpeili.com&#39;;
            $userinfo[&#39;password&#39;] = substr(md5($rs[&#39;unionid&#39;]),0,10);
            $userinfo[&#39;modelid&#39;] = isset($_POST[&#39;modelid&#39;]) ? intval($_POST[&#39;modelid&#39;]) : 10;
            $userinfo[&#39;regip&#39;] = ip();
            $userinfo[&#39;point&#39;] = $member_setting[&#39;defualtpoint&#39;] ? $member_setting[&#39;defualtpoint&#39;] : 0;
            $userinfo[&#39;amount&#39;] = $member_setting[&#39;defualtamount&#39;] ? $member_setting[&#39;defualtamount&#39;] : 0;
            $userinfo[&#39;regdate&#39;] = $userinfo[&#39;lastdate&#39;] = SYS_TIME;
            $userinfo[&#39;siteid&#39;] = $siteid;
            $userinfo[&#39;connectid&#39;] = isset($_SESSION[&#39;connectid&#39;]) ? $_SESSION[&#39;connectid&#39;] : &#39;&#39;;
            $userinfo[&#39;from&#39;] = isset($_SESSION[&#39;from&#39;]) ? $_SESSION[&#39;from&#39;] : &#39;&#39;;
            //手机强制验证
            //附表信息验证 通过模型获取会员信息
            if($member_setting[&#39;choosemodel&#39;]) {
                require_once CACHE_MODEL_PATH.&#39;member_input.class.php&#39;;
                require_once CACHE_MODEL_PATH.&#39;member_update.class.php&#39;;
                $member_input = new member_input($userinfo[&#39;modelid&#39;]);     
                $_POST[&#39;info&#39;] = array_map(&#39;new_html_special_chars&#39;,$_POST[&#39;info&#39;]);
                $user_model_info = $member_input->get($_POST[&#39;info&#39;]);                                       
            }
            if(pc_base::load_config(&#39;system&#39;, &#39;phpsso&#39;)) {
                $this->_init_phpsso();
                $status = $this->client->ps_member_register($userinfo[&#39;username&#39;], $userinfo[&#39;password&#39;], $userinfo[&#39;email&#39;], $userinfo[&#39;regip&#39;], $userinfo[&#39;encrypt&#39;]);
                echo $status;
                if($status > 0) {
                    $userinfo[&#39;phpssouid&#39;] = $status;
                    //传入phpsso为明文密码,加密后存入phpcms_v9
                    $password = $userinfo[&#39;password&#39;];
                    $userinfo[&#39;password&#39;] = password($userinfo[&#39;password&#39;], $userinfo[&#39;encrypt&#39;]); 
                    $userid = $this->db->insert($userinfo, 1);
                    if($member_setting[&#39;choosemodel&#39;]) {   //如果开启选择模型
                        $user_model_info[&#39;userid&#39;] = $userid;
                        //插入会员模型数据
                        $this->db->set_model($userinfo[&#39;modelid&#39;]);
                        $this->db->insert($user_model_info);
                    }
                    if($userid > 0) {
                        //执行登陆操作
                        if(!$cookietime) $get_cookietime = param::get_cookie(&#39;cookietime&#39;);
                        $_cookietime = $cookietime ? intval($cookietime) : ($get_cookietime ? $get_cookietime : 0);
                        $cookietime = $_cookietime ? TIME + $_cookietime : 0;
                        if($userinfo[&#39;groupid&#39;] == 7) {
                            param::set_cookie(&#39;_username&#39;, $userinfo[&#39;username&#39;], $cookietime);
                            param::set_cookie(&#39;email&#39;, $userinfo[&#39;email&#39;], $cookietime);                           
                        } else {
                            $phpcms_auth = sys_auth($userid."\t".$userinfo[&#39;password&#39;], &#39;ENCODE&#39;, get_auth_key(&#39;login&#39;));
                            //$this->db->update(array(&#39;groupid&#39;=> "6"), array(&#39;userid&#39;=>$userid));
                            $sql = "UPDATE `v9_member` SET `groupid`=&#39;6&#39; WHERE `userid` = $userid ";//修改用户级别为注册用户
                            mysql_query($sql);
                            param::set_cookie(&#39;auth&#39;, $phpcms_auth, $cookietime);
                            param::set_cookie(&#39;_userid&#39;, $userid, $cookietime);
                            param::set_cookie(&#39;_username&#39;, $userinfo[&#39;username&#39;], $cookietime);
                            param::set_cookie(&#39;_nickname&#39;, $userinfo[&#39;nickname&#39;], $cookietime);
                            param::set_cookie(&#39;_groupid&#39;, $userinfo[&#39;groupid&#39;], $cookietime);
                            param::set_cookie(&#39;cookietime&#39;, $_cookietime, $cookietime);
                            $forward = isset($_POST[&#39;forward&#39;]) && !empty($_POST[&#39;forward&#39;]) ? urldecode($_POST[&#39;forward&#39;]) : &#39;index.php?m=member&c=index&#39;;
                            //https 与 http 传递用户名   跨域跳转cooike
                            $url = "http://www.zhiliaoke.com.cn/set_cookie_www.php?nickname=".$_COOKIE[&#39;vuGYB__nickname&#39;]."&username=".$_COOKIE[&#39;vuGYB__username&#39;]."&userid=".$_COOKIE[&#39;vuGYB__userid&#39;]."&groupid=".$_COOKIE[&#39;vuGYB__groupid&#39;];
                            header("Location: ".$url.""); 
                        }
                    }
                }
            } else {
                showmessage(L(&#39;enable_register&#39;).L(&#39;enable_phpsso&#39;), &#39;/login.html&#39;);
            }
            showmessage(L(&#39;operation_failure&#39;), HTTP_REFERER);
        }
?>
Copy after login

Additional last step

is very important, and it is also the easiest to ignore! ! !

Several people are stuck here!

You need to determine whether the user is logged in through the wechat function around line 21 of the phpcms\modules\member\classes\foreground.class.php file

How to implement WeChat login in phpcms

PHP Chinese website , a large number of free PHPCMS tutorials, welcome to learn online!

The above is the detailed content of How to implement WeChat login in phpcms. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Ouyi Exchange app domestic download tutorial Ouyi Exchange app domestic download tutorial Mar 21, 2025 pm 05:42 PM

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

What is the difference between H5 page production and WeChat applets What is the difference between H5 page production and WeChat applets Apr 05, 2025 pm 11:51 PM

H5 is more flexible and customizable, but requires skilled technology; mini programs are quick to get started and easy to maintain, but are limited by the WeChat framework.

What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? Apr 01, 2025 pm 10:48 PM

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

How to solve the problem of JS resource caching in enterprise WeChat? How to solve the problem of JS resource caching in enterprise WeChat? Apr 04, 2025 pm 05:06 PM

Discussion on the JS resource caching issue of Enterprise WeChat. When upgrading project functions, some users often encounter situations where they fail to successfully upgrade, especially in the enterprise...

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to choose H5 and applets How to choose H5 and applets Apr 06, 2025 am 10:51 AM

The choice of H5 and applet depends on the requirements. For applications with cross-platform, rapid development and high scalability, choose H5; for applications with native experience, rich functions and platform dependencies, choose applets.

Detailed tutorial on how to buy and sell Binance virtual currency Detailed tutorial on how to buy and sell Binance virtual currency Mar 18, 2025 pm 01:36 PM

This article provides a brief guide to buying and selling of Binance virtual currency updated in 2025, and explains in detail the operation steps of virtual currency transactions on the Binance platform. The guide covers fiat currency purchase USDT, currency transaction purchase of other currencies (such as BTC), and selling operations, including market trading and limit trading. In addition, the guide also specifically reminds key risks such as payment security and network selection for fiat currency transactions, helping users to conduct Binance transactions safely and efficiently. Through this article, you can quickly master the skills of buying and selling virtual currencies on the Binance platform and reduce transaction risks.

See all articles