PHPCMS 本身不内置 OAuth 机制,但可通过扩展实现第三方登录。首先基于其 member 模块构建本地账号体系,再通过引入 SDK 或手动实现 OAuth2 流程,完成与微信、QQ 等平台的授权对接。具体步骤包括:在开放平台注册应用获取 App ID 与 App Secret,配置回调地址如 http://yourdomain/index.php?m=member&c=oauth&a=callback&platform=qq,在 PHPCMS 的 /phpcms/modules/member/ 下创建 oauth.php 控制器,实现 qq_login() 方法跳转至授权页,callback() 方法处理回调并获取 code。接着通过 code 请求 access_token,再用 access_token 获取 openid 和用户信息,查询 v9_member 表是否已存在该 openid 绑定记录,若无则自动创建新用户并保存昵称、头像、openid 等信息,若有则直接登录。最后调用 _set_session_and_cookie() 设置 session 与 cookie 完成登录态维持。需注意确保回调地址一致、对 openid 建立唯一索引、避免敏感数据泄露,并建议增加 CSRF 防护及支持已有账号绑定功能,以提升安全性和用户体验。尽管 PHPCMS 较为陈旧,但通过合理扩展仍可实现现代 OAuth 登录功能,关键在于理解其 MVC 架构与用户系统设计。

PHPCMS 本身并不内置完整的 OAuth 授权机制(如微信、QQ、微博等第三方登录),但可以通过扩展方式实现 OAuth 授权功能。下面介绍如何在 PHPCMS 中实现授权逻辑,以及如何配置使用 OAuth 登录。
PHPCMS 原生采用的是本地账号体系,用户注册、登录、权限控制都基于其自带的 member 模块。用户信息存储在数据库表如 v9_member 中,登录通过 session 和 cookie 维护状态。
若要实现第三方授权登录(如 OAuth2),需要在原有体系基础上进行扩展:
以接入微信开放平台或 QQ 登录为例,实现 OAuth2 登录的基本流程如下:
立即学习“PHP免费学习笔记(深入)”;
http://yourdomain/index.php?m=member&c=oauth&a=callback&platform=qq
/phpcms/modules/member/ 下新增控制器文件,如 oauth.php
在 phpcms/modules/member/oauth.php 中:
class oauth {
public function qq_login() {
$appid = '你的AppID';
$redirect_uri = urlencode('http://yourdomain/index.php?m=member&c=oauth&a=callback&platform=qq');
$url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id={$appid}&redirect_uri={$redirect_uri}";
header("Location: $url");
}
public function callback() {
$code = $_GET['code'];
$platform = $_GET['platform'];
if ($platform == 'qq') {
// 获取 access_token
$token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=你的ID&client_secret=你的密钥&code={$code}&redirect_uri=回调地址";
$response = file_get_contents($token_url);
parse_str($response, $token_arr);
$access_token = $token_arr['access_token'];
// 获取 openid
$openid_url = "https://graph.qq.com/oauth2.0/me?access_token={$access_token}";
$openid_str = file_get_contents($openid_url);
preg_match('/"openid":"(\w+)"/', $openid_str, $matches);
$openid = $matches[1];
// 获取用户信息
$user_info_url = "https://graph.qq.com/user/get_user_info?access_token={$access_token}&oauth_consumer_key=你的ID&openid={$openid}";
$user_data = json_decode(file_get_contents($user_info_url), true);
// 查询或创建本地用户
$member_db = pc_base::load_model('member_model');
$local_user = $member_db->get_one(['qq_openid' => $openid]);
if (!$local_user) {
$new_user = [
'username' => 'qq_' . $openid,
'nickname' => $user_data['nickname'],
'qq_openid' => $openid,
'face' => $user_data['figureurl_qq_2'],
'password' => '', // 第三方登录无密码
'reg_time' => SYS_TIME
];
$userid = $member_db->insert($new_user, true);
$local_user = $new_user;
$local_user['userid'] = $userid;
}
// 执行登录
$this->_set_session_and_cookie($local_user['userid'], $local_user['username']);
header("Location: /index.php");
}
}
private function _set_session_and_cookie($userid, $username) {
$_SESSION['userid'] = $userid;
$_SESSION['username'] = $username;
param::set_cookie('userid', $userid);
param::set_cookie('username', $username);
}
}
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号