1. Apply app ID and app KEY to QQ Internet. Address: http://connect.qq.com/Detailed Note method here is no longer to repeat.
Note that you can write multiple callback addresses here and you need to be consistent with your configuration file: HTTP://WWW.***.COM/LOGIN/QQ_LOGIN_CB;HTTP://WWW.***.COM/INDEX/QQ_BC
3.qqhelper.class.php This file please put in the thinkphp Core report path: ThinkphpExtendLibraryORGNetQQHelper.class.php, this file for QQ login Core class.
4. Configure the configuration file as follows:
Web site URL Path
Define (' Website_url ', ' http://www.***.com ');
Application ID This is in http://connect.qq.com/application
Define (' app_id ', *********);
Apply key This is in http://connect.qq.com/application
Define (' App_key ', ' ****9676d9bec3bf0f0d53ff4ff6**** ');
QQ Login Callback Address
Define (' App_login_cpath ', Website_url. '/LOGIN/QQ_LOGIN_CB ');//The path here should be consistent with the callback interface on the QQ interconnect
Bind QQ Callback Address
Define (' App_bind_cpath ', Website_url. '/INDEX/QQ_BC ');//The path here should be consistent with the callback interface on the QQ interconnect
A list of available authorizations to display to users when requesting authorization
Define (' App_scope ', ' Get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo ') ;
Whether the picture is localized
Define (' Avatar_local ', false);
Session Settings
Define (' Ses_token_name ', ' Qq_token '); Token name
Define (' Ses_openid_name ', ' Qq_openid '); The OpenID name of QQ user
Define (' Ses_state_name ', ' qq_state ');//Status name
5.php Code
1.commonaction.class.php Code
Class Commonaction extends Action
{
Public Function _initialize () {
Header ("content-type:text/html; Charset=utf-8 ");
Header (' Content-type:application/json; Charset=utf-8 ');
$systemConfig = include web_root. ' common/systemconfig.php ';
$this->assign ("site", $systemConfig);
if (Empty ($_session[' info '] [' username ']))
{
if (Empty ($_session[' Qq_openid '))
{
$L = "<li><a href=". $systemConfig [' Web_root ']. " Register/index> registration </a></li> ";
$L. = "<li><a href= ' javascript:void (0); ' id= ' login ' > Login </a></li> ';
$L. = "<li><a href= '". $systemConfig [' Web_root ']. " Login/qq_login ' ></a></li> ';
}else{
if ($_session[' info '] [' bind ']== ' N ')
{
$L = "<li><a href= '". $systemConfig [' Web_root ']. " Login/loginout ' > Exit </a></li> ';
$L. = "<li><a href= '". $systemConfig [' Web_root ']. " Center/index ' > Member Center </a></li> ';
$L. = "<li><a href= '". $systemConfig [' Web_root ']. " Index/readtemplate?source=bind_uname ' > Please bind user name </a></li>;
$L. = "<li>" $_session[' info ' [' nickname ']. " </li> ";
$L. = "<li></li> ";
}else{
$L = "<li><a href= '". $systemConfig [' Web_root ']. " Login/loginout ' > Exit </a></li> ';
$L. = "<li><a href= '". $systemConfig [' Web_root ']. " Center/index ' > Member Center </a></li> ';
$L. = "<li><a href= '/index/readtemplate?source=bind_uname ' > Please bind user name </a></li>";
$L. = "<li>" $_session[' info ' [' nickname ']. " </li> ";
$L. = "<li></li> ";
}
}
}else{
$L = "<li><a href= '". $systemConfig [' Web_root ']. " Login/loginout ' > Exit </a></li> ';
if (Empty ($_session[' info '] [' open_id ']))
{
$L. = "<li><a href= '". $systemConfig [' Web_root ']. " Index/qq_bind ' > Binding QQ number </a></li>;
}else{
$L. = "<li><a href=". $systemConfig [' Web_root ']. " Index/qq_unbind ' > Untied QQ number </a></li>;
}
$L. = "<li><a href= '". $systemConfig [' Web_root ']. " Center/index ' > Member Center </a></li> ';
$L. = "<li> Hello:" $_session[' info ' [' username ']. " </li> ";
}
$this->assign ("L", $L);
}
}
2. Realize QQ Login code loginaction must inherit Commonaction
Class Loginaction extends Commonaction {
Private $user;
Private $qqHelper;
function _initialize () {
$this->user = M (' member ');
Import (' ORG.Net.QQHelper ');
$this->qqhelper = new Qqhelper ();
}
QQ Login
Public Function Qq_login ()
{
$this->qqhelper->login (app_id, App_scope, App_login_cpath);
}
Callback for QQ Login
function Qq_login_cb () {
$this->qqhelper->callback (App_login_cpath);
$this->qqhelper->get_openid ();
Go to the database first to find OpenID exists not
$temp _data = $this->user->where (Array (' open_id ' => $_session [ses_openid_name]))->find ();
if (Empty ($temp _data[' open_id ')) {
Prove that QQ has not been logged in the site
$userInfo = Json_decode ($this->qqhelper->get_user_info (), true);
$data [' open_id '] = $_session [Ses_openid_name];
$data [' nickname '] = $userInfo [' nickname '];
$data [' avatar '] = $userInfo [' figureurl_2 '];
$data [' login_ip '] = Get_client_ip ();
$data [' login_time '] = time ();
$data [' reg_date '] = time ();
$this->user->add ($data);
Write information to session
$temp _data = $this->user->where (Array (' UID ' => $this->user->getlastinsid ()))->find ();
$_session[' info ']= $temp _data;
} else {
$_session[' info ']= $temp _data;
}
$this->redirect (' Index/index ');
}
Exit
Public Function Loginout ()
{
unset ($_session[' info ']);
Session_destroy ();
$this->redirect (' Index/index ');
}
}
3.QQ and local user binding indexaction must inherit Commonaction
Class Indexaction extends Commonaction {
Private $user;
Private $qqHelper;
function _initialize () {
Parent::_initialize ();
$this->user = M (' member ');
Import (' ORG.Net.QQHelper ');
$this->qqhelper = new Qqhelper ();
}
/**QQ Login **/
Binding user name refers to the use of QQ login users, there is no site account number and password
function Bind_uname () {
$data [' username '] = $_post [' uname '];
$data [' pwd '] = MD5 (MD5 ($_post [' pwd ']));
$data [' bind '] = ' Y ';
$map [' open_id '] = $_session [Ses_openid_name]; The OpenID of QQ
Find out if the OpenID has already been bound to a user name without
$stat = $this->user->where ($map)->find ();
if ($stat [' username ']!= ' | | | $stat [' username ']!= null) {
$this->error (' You have already bound the username, please do not bind it again ');
}
$res = $this->user->where ($map)->save ($data);
Update session
$_session[' info ' [' Bind ']= ' Y ';
if ($res) {
$this->success (' Bind user success ');
} else {
$this->error (' Bind failed ');
}
}
Binding QQ number is mainly registered through this site users
function Qq_bind () {
First to determine if the account is bound to the QQ number
$map [' open_id '] = $_session [Ses_openid_name];
$res = $this->user->where ($map)->find ();
if ($res!= null) {
$this->error (' You have already bound QQ account number, if want to change please unbind first! ' );
}
$this->qqhelper->login (app_id, App_scope, App_bind_cpath); Note that the callback address here differs from the login callback address
}
Callback address for binding QQ number
function Qq_bc () {
$tmp _ses = $_session [Ses_openid_name]; Save before sesion to avoid session being overwritten
$this->qqhelper->callback (App_bind_cpath); Note that the callback address here differs from the login callback address
$this->qqhelper->get_openid ();
$userInfo = Json_decode ($this->qqhelper->get_user_info (), true);
First to determine whether this QQ number has been bound to the account no
$res = $this->user->where (Array (' open_id ' => $_session [ses_openid_name]))->find ();
if ($res!= null) {
$_session [Ses_openid_name] = $tmp _ses;
$this->error (' The QQ number has already been bound to the account number, do not bind again ',/esoxue.php);
}
$map [' uid '] = $_session[' info '] [' uid '];
$data [' open_id '] = $_session [Ses_openid_name];
$data [' avatar '] = $userInfo [' figureurl_2 '];
$data [' bind '] = ' Y ';
$stat = $this->user->where ($map)->save ($data);
if ($stat > 0) {
$this->success (' bound QQ number is successful! ',/esoxue.php);
} else {
$this->error (' Bind QQ failed ',/esoxue.php);
}
}
Untied QQ number
function Qq_unbind () {
$map [' uid '] = $_session[' info '] [' uid '];
$data [' open_id '] = ';
$res = $this->user->where ($map)->save ($data);
if ($res > 0) {
$this->success (' Untied success! ',/esoxue.php);
} else {
$this->error (' untied Failure ',/esoxue.php);
}
}
Read templates
function Readtemplate () {
$DOTPL = $_get [' source '];
if ($dotpl = = ' Bind_uname ') {
$this->display (' bind_uname ');
}
}
/**QQ Login **/
}
Output {$L} header in header is include file
6. The database adds the following fields to your membership:
Nickname, Avatar, open_id, bind
CREATE TABLE IF not EXISTS ' Lw_member ' (
' UID ' int (one) not NULL auto_increment,
' Nickname ' varchar DEFAULT NULL COMMENT ' user nickname ',
' pwd ' char (+) DEFAULT NULL COMMENT ' password ',
' Avatar ' varchar (MB) DEFAULT NULL COMMENT ' user Avatar ',
' login_ip ' varchar DEFAULT NULL COMMENT ' logon IP ',
' Login_time ' int () DEFAULT NULL COMMENT ' logon time ',
' username ' varchar not NULL COMMENT ' username ',
' open_id ' varchar not NULL,
' Bind ' enum (' n ', ' Y ') not NULL DEFAULT ' n ',
PRIMARY KEY (' uid ')
Engine=myisam DEFAULT Charset=utf8 row_format=dynamic comment= ' website front desk member table ' auto_increment=6;
Bind this to the user template code:
<form action= "{: U (' Index/bind_uname ')}" method= "POST" >
<p style= "Margin:0;text-align:right;margin-right:10px;cursor:pointer;" ></p>
<p> </p>
<p><input type= "text" name= "uname" class= "input" placeholder= "Please enter user name"/></p>
<p> </p>
<p><input type= "password" name= "pwd" class= "input" placeholder= "Please enter password"/></p>
<p> </p>
<p><input type= "Submit" name= "submit" value= "binding" class= "Submit"/></p>
</form>
Temporary Demo Address: http://sheng.weilingdao.com
All of the above code can be changed according to the actual situation. Need to note: QQ Login needs curl support