首先,登录QQ互联平台获取QQ一键登录所需的Appkey和Appsecret。
2、下载QQ登录用的SDK软件包,可以上QQ互联官方网下载。这里用的是Connect_PHP_SDK_for_OAuth2_V1.1.zip
3、在登录页面放置QQ登录的图标。
functiontoQzoneLogin()
{
varA=window.open("oauth/qq_login.php","TencentLogin","width=450,height=320,menubar=0,scrollbars=1,resizab
le=1,status=1,titlebar=0,toolbar=0,location=1");
}
4、修改comm/config.php中的4个变量
$_SESSION["appid"];
$_SESSION["appkey"];
$_SESSION["callback"];
$_SESSION["scope"];
5、以下是代码的【实现】
oauth/qq_login.php
require_once("../comm/config.php");
function qq_login($appid,$scope, $callback)
{
$_SESSION['state'] = md5(uniqid(rand(),TRUE)); //CSRF protection
. $appid ."&redirect_uri=" . urlencode($callback)
. "&state=" .$_SESSION['state']
. "&scope=".$scope;
header("Location:$login_url");
}
//用户点击qq登录按钮调用此函数
qq_login($_SESSION["appid"],$_SESSION["scope"], $_SESSION["callback"]);
?>
//应用的APPID
$app_id = "YOUR_APP_ID";
//应用的APPKEY
$app_secret = "YOUR_APP_KEY";
//成功授权后的回调地址
$my_url = "YOUR_REDIRECT_URL";
//Step1:获取Authorization Code
session_start();
$code = $_REQUEST["code"];
if(empty($code))
{
//state参数用于防止CSRF***,成功授权后回调时会原样带回
$_SESSION['state'] =md5(uniqid(rand(), TRUE));
//拼接URL
. $app_id. "&redirect_uri=" . urlencode($my_url) . "&state="
.$_SESSION['state'];
echo("");
}
//Step2:通过Authorization Code获取Access Token
if($_REQUEST['state'] == $_SESSION['state'])
{
//拼接URL
. "client_id=" .$app_id . "&redirect_uri=" . urlencode($my_url)
."&client_secret=" . $app_secret . "&code=" . $code;
$response =file_get_contents($token_url);
if (strpos($response,"callback") !== false)
{
$lpos =strpos($response, "(");
$rpos =strrpos($response, ")");
$response =substr($response, $lpos + 1, $rpos - $lpos -1);
$msg =json_decode($response);
if(isset($msg->error))
{
echo "
error:
" . $msg->error;echo "
msg :
" .$msg->error_description;exit;
}
}
//Step3:使用Access Token来获取用户的OpenID
$params = array();
parse_str($response, $params);
$params['access_token'];
$str =file_get_contents($graph_url);
if (strpos($str,"callback") !== false)
{
$lpos =strpos($str, "(");
$rpos =strrpos($str, ")");
$str =substr($str, $lpos + 1, $rpos - $lpos -1);
}
$user = json_decode($str);
if (isset($user->error))
{
echo"
error:
" . $user->error;echo"
msg :
" .$user->error_description;exit;
}
echo("Hello " .$user->openid);
}
else
{
echo("The state does notmatch. You may be a victim of CSRF.");
}
?>
以上是写好的接口,实际上我们只需要在回调页面callback.php修改就可以。
require_once("../config.php");
require_once("../qqConnectAPI.php");
require '../config.inc.php';
require '../uc_client/client.php';
require '../api/uc.php';
$qc = new QC();
$acs=$qc->qq_callback();
global $SESSION;
$SESSION->acs=$acs;
//echo $SESSION->acs."
";
$oid = $qc->get_openid();
$SESSION->oid=$oid;
//echo $SESSION->oid."
";
$qc = new QC($acs,$oid);
$uinfo = $qc->get_user_info();
//var_dump($uinfo);
$SESSION->nickname=$uinfo['nickname'];
$username=$SESSION->nickname;
$link=@mysql_connect("localhost","root","root");
mysql_select_db("moodle");
mysql_query('setnames utf8');
$sql1="select* from mdl_user where username='".$uinfo['nickname']."'";
echo $sql1;
$res1=mysql_query($sql1);
$result=mysql_num_rows($res1);
echo $result;
if($result==0){
$sql="insert into mdl_user(id,auth,username,deleted,mnethostid,firstname,openid) values(null,'manual','".$uinfo['nickname']."',0,1,'".$uinfo['nickname']."','$oid')";
$res=mysql_query($sql);
$mysqli=newmysqli("localhost","root","root","discuz");
$mysqli->query("setnames utf8");
$sql2="insertinto cdb_uc_members (uid,username) values(null,'".$uinfo['nickname']."')";
$mysqli->query($sql2);
}
$user =$DB->get_record('user', array('openid' =>$oid, 'deleted' => 0,'mnethostid' => $CFG->mnet_localhost_id));
//var_dump($user);
$userid=$user->id;
//$password=$user->password;
// echo$password;
//echo$uid;
$mysqli=newmysqli("localhost","root","root","discuz");
$mysqli->query("setnames utf8");
$sql3="select* from cdb_uc_members where username='".$uinfo['nickname']."'";
$res= $mysqli->query($sql3);
$row=mysqli_fetch_assoc($res);
$uid=$row['uid'];
$result2= mysqli_num_rows($res);
if($result2 != 0) {
$username=$uinfo['nickname'];
//list($uid, $username2,$password2, $email)= uc_user_login($username,$password=null);
//ucenter表有此用户,设置Cookie,登录成功,并通知其它应用同步登录
setcookie('Mdl_auth', uc_authcode($uid."\t".$username,'ENCODE'));
//生成同步登录的代码
$ucsynlogin = uc_user_synlogin($uid);
echo $ucsynlogin.'';
}
//add_to_log(SITEID,'auth_qq', '', '', $username . '/' . $useremail . '/' . $userid);
//$user =authenticate_user_login($username,$password);
//var_dump($user);
complete_user_login($user);
echo "
window.opener.location.reload();
window.close();
";
?>