search
Homephp教程php手册phpcms V9实现QQ登陆OAuth2.0

phpcmsV9使用的QQ登陆依然是OAuth1.0,但现在腾讯已经不审核使用OAuth1.0的网站了。这对于使用pc的站长来讲是一个无比巨大的坑。经过对phpcms论坛的一位同学做的插件进行修改,现在完美实现QQ登陆,暂无发现bug。

实现步骤:

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

 

  1. //QQ登陆oAuth2.0

  2. ublic function public_qq_login2(){  

  3. $appid = pc_base::load_config('system', 'qq_appid');  

  4. $appkey = pc_base::load_config('system', 'qq_appkey');  

  5. $callback = pc_base::load_config('system', 'qq_callback');  

  6.  pc_base::load_app_class('qqOAuth2','',0);  

  7. $info = new qqOAuth2($appid,$appkey,$callback);  

  8. $this->_session_start();  

  9. if (!isset($_GET['code'])){  

  10. $info->redirect_to_login();  

  11.  }else{  

  12. $code = $_GET['code'];  

  13. $_SESSION['openid'] = $info->get_openid($code);//调取QQ openid值

  14. if(!emptyempty($_SESSION['openid'])){  

  15. $r = $this->db->get_one(array('connectid'=>$_SESSION['openid'],'from'=>'qq'));  

  16. if(!emptyempty($r)){  

  17. //QQ已存在于数据库,则直接转向登陆操作

  18. $password = $r['password'];  

  19. $this->_init_phpsso();  

  20. $synloginstr = $this->client->ps_member_synlogin($r['phpssouid']);  

  21. $userid = $r['userid'];  

  22. $groupid = $r['groupid'];  

  23. $username = $r['username'];  

  24. $nickname = emptyempty($r['nickname']) ? $username : $r['nickname'];  

  25. $this->db->update(array('lastip'=>ip(), 'lastdate'=>SYS_TIME, 'nickname'=>$me['name']),array('userid'=>$userid));  

  26. if(!$cookietime) $get_cookietime = param::get_cookie('cookietime');  

  27. $_cookietime = $cookietime ? intval($cookietime) : ($get_cookietime ? $get_cookietime : 0);  

  28. $cookietime = $_cookietime ? TIME + $_cookietime : 0;  

  29. $phpcms_auth_key = md5(pc_base::load_config('system', 'auth_key').$this->http_user_agent);  

  30. $phpcms_auth = sys_auth($userid."\t".$password, 'ENCODE', $phpcms_auth_key);  

  31.  param::set_cookie('auth', $phpcms_auth, $cookietime);  

  32.  param::set_cookie('_userid', $userid, $cookietime);  

  33.  param::set_cookie('_username', $username, $cookietime);  

  34.  param::set_cookie('_groupid', $groupid, $cookietime);  

  35.  param::set_cookie('cookietime', $_cookietime, $cookietime);  

  36.  param::set_cookie('_nickname', $nickname, $cookietime);  

  37. $forward = isset($_GET['forward']) && !emptyempty($_GET['forward']) ? $_GET['forward'] : 'index.php?m=member&c=index';  

  38.  showmessage(L('login_success').$synloginstr, $forward);  

  39.  }else{  

  40. //未存在于数据库中,跳去完善资料页面。页面预置用户名(QQ返回是UTF8编码,如有需要进行转码)

  41. $user = $info->get_user_info();  

  42. $_SESSION['connectid'] = $_SESSION['openid'];  

  43. $_SESSION['from'] = 'qq';  

  44. if(CHARSET != 'utf-8') {//转编码

  45. $connect_username = iconv('utf-8', CHARSET, $user['nickname']);    

  46. }  

  47. include template('member', 'connect');  

  48.  }  

  49.  }  

  50.  }  

  51.  }  

②把附件的qqOAuth2.class.php文件放在 \phpcms\modules\member\classes\ 文件夹里。

③在后台找到connect设置,把QQ号码登陆的回调地址改成http://php100.com/index.php?m=member&c=index&a=public_qq_login2 (把我的域名换成你自己的)

④填好App ID和App key,然后更新缓存。

⑤登陆phpmyadmin,找到v9_member这张表的connectid字段(或者自定义的字段),把类型改成“char(255)”,因为phpcms默认是15位的。所以我们直接彻底修改成255位,这个不管是腾讯的其它第三方网站帐号(例如淘宝、支付宝、百度、360等其它第三方帐号),都可以完成数据绑定。(此条添加于7月27号,由网友liuyang814发邮件提醒)

后期会把新浪微博及腾讯微博的代码放上来。phpcms的周边代码太少了,网上找都找不到。



Statement
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 Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.