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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Thanks to netizens Qing Qiechensi, HH_KK, Satomi Ishihara and Wu Yanzu of South China for submitting clues! According to news on September 2, there are recent rumors that "iPhone 16 may not support WeChat." In response to this, a reporter from Shell Finance called Apple's official hotline. Apple's technical consultant in China responded that whether iOS systems or Apple devices can continue to use WeChat, and WeChat The issue of whether it can continue to be listed and downloaded on the Apple App Store requires communication and discussion between Apple and Tencent to determine the future situation. Software App Store and WeChat Problem Description Software App Store technical consultant pointed out that developers may need to pay fees to put software on the Apple Store. After reaching a certain number of downloads, Apple will need to pay corresponding fees for subsequent downloads. Apple is actively communicating with Tencent,

DeepSeek: A powerful AI image generation tool! DeepSeek itself is not an image generation tool, but its powerful core technology provides underlying support for many AI painting tools. Want to know how to use DeepSeek to generate images indirectly? Please continue reading! Generate images with DeepSeek-based AI tools: The following steps will guide you to use these tools: Launch the AI Painting Tool: Search and open a DeepSeek-based AI Painting Tool (for example, search "Simple AI"). Select the drawing mode: select "AI Drawing" or similar function, and select the image type according to your needs, such as "Anime Avatar", "Landscape"

Rumors of WeChat supporting iPhone 16 were debunked. Thanks to netizens Xi Chuang Jiu Shi and HH_KK for submitting clues! According to news on September 2, there are rumors today that WeChat may not support iPhone 16. Once the iPhone is upgraded to the iOS 18.2 system, it will not be able to use WeChat. According to "Daily Economic News", it was learned from people familiar with the matter that this rumor is a rumor. Apple's response: According to Shell Finance, Apple's technical consultant in China responded that the issue of whether WeChat can continue to be used on iOS systems or Apple devices, and whether WeChat can continue to be listed and downloaded in the Apple App Store, needs to be resolved between Apple and Tencent. Only through communication and discussion can we determine the future situation. Currently, Apple is actively communicating with Tencent to confirm whether Tencent will continue to

Gate.io, a leading cryptocurrency trading platform founded in 2013, provides Chinese users with a complete official Chinese website. The website provides a wide range of services, including spot trading, futures trading and lending, and provides special features such as Chinese interface, rich resources and community support.

The OKX trading platform offers a variety of rates, including transaction fees, withdrawal fees and financing fees. For spot transactions, transaction fees vary according to transaction volume and VIP level, and adopt the "market maker model", that is, the market charges a lower handling fee for each transaction. In addition, OKX also offers a variety of futures contracts, including currency standard contracts, USDT contracts and delivery contracts, and the fee structure of each contract is also different.

Gate.io (Sesame Open Door) is the world's leading cryptocurrency trading platform. This article provides a complete tutorial on spot trading of Gate.io. The tutorial covers steps such as account registration and login, KYC certification, fiat currency and digital currency recharge, trading pair selection, limit/market transaction orders, and orders and transaction records viewing, helping you quickly get started on the Gate.io platform for cryptocurrency trading. Whether a beginner or a veteran, you can benefit from this tutorial and easily master the Gate.io trading skills.

Gateio Exchange app download channels for old versions, covering official, third-party application markets, forum communities and other channels. It also provides download precautions to help you easily obtain old versions and solve the problems of discomfort in using new versions or device compatibility.

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.
