首頁 >php教程 >php手册 >【新手】让新手程序猿也会ThinkPHP for QQ登陆

【新手】让新手程序猿也会ThinkPHP for QQ登陆

WBOY
WBOY原創
2016-06-07 11:38:271831瀏覽

【新手】一个控制器让更多的新手程序猿了解QQ登陆原理
 //----------------------------------------------------------<br>  //声明: 本代码并非完美,也许存在不佳之处,请放肆吐槽!<br>  //作者: 小曾<br>  //扣扣: 839024615<br>  //网址: www.yun8888.net<br>  //关于我的ThinkPHP http://www.thinkphp.cn/u/87696.html<br>  //----------------------------------------------------------<br> <br> //登陆模块<br> //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br> //QQ登陆SDK下载地址 http://connect.qq.com/<br> //申请时填写回调地址 域名/index.php/Login/qq_callback.html<br> //下载好SDK解压到 /ORG/  类下目录 或自定的目录<br> //直接访问SDK下的index.php按提示进行安装<br> //配置时填写回调地址 域名/index.php/Login/qq_callback.html<br> //获得权限全打勾,这个可以随便,如果你要发微博关注等就勾上<br> //配置完了,把API之外的文件与目录删除 我把API改成Connect<br> //把qqConnectAPI.php 改成 qqConnectAPI.class.php<br> //引入方法 import('@.ORG.Connect.qqConnectAPI');<br> //当然用别的方法也一样,由个人喜欢而定哈<br> //下面根据代码说明 了解QQ登陆的详细原理吧<br> //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br> class LoginAction extends Action {<br>     public function _initialize(){<br>         //引入QQ登陆类<br>         import('@.ORG.Connect.qqConnectAPI');<br>         //实例化<br>         $this->QC = new QC();<br>     }<br>     //开始登陆<br>     public function qq_login(){<br>         $this->QC->qq_login();<br>     }<br>     //回调<br>     public function qq_callback(){<br>         $token  = $this->QC->qq_callback();<br>         $openid = $this->QC->get_openid();<br>         $QC = new QC($token,$openid);<br>         $arr = $QC->get_user_info();<br>         <br>         $db=M("Member");<br>         $where['qq_openid']=$openid;<br>         //判断此QQ是否注册 唯一标识符openid 在表里加了个qq_openid为唯一哈<br>         $isqq=$db->where($where)->find();<br>         if($isqq){<br>             //写入登陆状态<br>             session('id', $isqq['id']);<br>             session('account', $isqq['account']); <br>             session('nickname', $isqq['account']);<br>             session('email', $isqq['email']); <br>             session('lastLoginTime', $isqq['lastLoginTime']); <br>             session('login_count', $isqq['login_count']); <br>             $this->success('登陆成功!',U('Member/index'));<br>         }else{<br>             $data['qq_openid']=$openid; //QQ登陆唯openid<br>             $data['account']=$arr['nickname']; //用户名<br>             $data['nickname']=$arr['nickname']; //网名<br>             $data['thumb']=$arr['figureurl_2']; //头像<br>             $data['status']='1'; //用户状态为启用<br>             //如果用户名存在<br>             $name['account']=$arr['nickname'];<br>             $isname=$db->where($name)->find();<br>             if($isname){<br>                 //用户名存在添加随机数<br>                 $data['account']=$arr['nickname']."_".rand(1000,9999);<br>             }<br>             //判断是否写入成功<br>             if($id=$db->add($data)){<br>                 //写入登陆状态<br>                 session('id', $id);<br>                 session('account', $data['account']);<br>                 session('nickname', $data['account']);<br>                 session('email', '未填写');<br>                 session('lastLoginTime', time());<br>                 session('login_count', '1');<br>                 $this->success('注册成功!',U('Member/index'));<br>             }else{<br>                 $this->error('注册失败!',U('Member/login'));<br>             }    <br>         }<br>     }<br> }<br> <br>  //----------------------------------------------------------<br>  //声明: 本代码并非完美,也许存在不佳之处,请放肆吐槽!<br>  //作者: 小曾<br>  //扣扣: 839024615<br>  //网址: www.yun8888.net<br>  //关于我的ThinkPHP http://www.thinkphp.cn/u/87696.html<br>  //----------------------------------------------------------

AD:真正免费,域名+虚机+企业邮箱=0元

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn