点击登录
  • 欢迎访问无限星辰技术博客,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏无限星辰吧
  • 好集导航开张了,传送门:好集导航

phpcms V9实现QQ登陆OAuth2.0

Phpcms知识库 crx349 4904次浏览 0个评论 扫描二维码

①打开\phpcms\modules\member\index.php文件,把下面的代码找个地方塞进去(可以直接在pc默认的QQ登陆下面)。

//QQ登陆oAuth2.0
public function public_qq_login2(){
$appid = pc_base::load_config(‘system’, ‘qq_appid’);
$appkey = pc_base::load_config(‘system’, ‘qq_appkey’);
$callback = pc_base::load_config(‘system’, ‘qq_callback’);
pc_base::load_app_class(‘qqOAuth2’,”,0);
$info = new qqOAuth2($appid,$appkey,$callback);
$this->_session_start();
if (!isset($_GET[‘code’])){
$info->redirect_to_login();
}else{
$code = $_GET[‘code’];
$_SESSION[‘openid’] = $info->get_openid($code);//调取QQ openid值
if(!empty($_SESSION[‘openid’])){
$r = $this->db->get_one(array(‘connectid’=>$_SESSION[‘openid’],’from’=>’qq’));
if(!empty($r)){
//QQ已存在于数据库,则直接转向登陆操作
$password = $r[‘password’];
$this->_init_phpsso();
$synloginstr = $this->client->ps_member_synlogin($r[‘phpssouid’]);
$userid = $r[‘userid’];
$groupid = $r[‘groupid’];
$username = $r[‘username’];
$nickname = empty($r[‘nickname’]) ? $username : $r[‘nickname’];
$this->db->update(array(‘lastip’=>ip(), ‘lastdate’=>SYS_TIME, ‘nickname’=>$me[‘name’]), array(‘userid’=>$userid));
if(!$cookietime) $get_cookietime = param::get_cookie(‘cookietime’);
$_cookietime = $cookietime ? intval($cookietime) : ($get_cookietime ? $get_cookietime : 0);
$cookietime = $_cookietime ? TIME + $_cookietime : 0;
$phpcms_auth_key = md5(pc_base::load_config(‘system’, ‘auth_key’).$this->http_user_agent);
$phpcms_auth = sys_auth($userid.”\t”.$password, ‘ENCODE’, $phpcms_auth_key);
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(‘cookietime’, $_cookietime, $cookietime);
param::set_cookie(‘_nickname’, $nickname, $cookietime);
$forward = isset($_GET[‘forward’]) && !empty($_GET[‘forward’]) ? $_GET[‘forward’] : ‘index.php?m=member&c=index’;
showmessage(L(‘login_success’).$synloginstr, $forward);
}else{
//未存在于数据库中,跳去完善资料页面。页面预置用户名(QQ返回是UTF8编码,如有需要进行转码)
$user = $info->get_user_info();
$_SESSION[‘connectid’] = $_SESSION[‘openid’];
$_SESSION[‘from’] = ‘qq’;
if(CHARSET != ‘utf-8’) {//转编码
$connect_username = iconv(‘utf-8’, CHARSET, $user[‘nickname’]);
}
include template(‘member’, ‘connect’);
}
}
}
}

②把附件的qqOAuth2.class.php文件放在 \phpcms\modules\member\classes\ 文件夹里。qqOAuth2.class
③在后台找到connect设置,把QQ号码登陆的回调地址改成http://www.xmspacep.net/index.php?m=member&c=index&a=public_qq_login2 (把我的域名换成你自己的)
④填好App ID和App key,然后更新缓存。


无限星辰 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明phpcms V9实现QQ登陆OAuth2.0!
喜欢 (0)
[]
分享 (0)

您必须 登录 才能发表评论!