QQ一鍵登錄實現

首先,登錄QQ互聯平臺http://connect.qq.com/manage/獲取QQ一鍵登錄所需的AppkeyAppsecret


2、下載QQ登錄用的SDK軟件包,可以上QQ互聯官方網下載。這裏用的是Connect_PHP_SDK_for_OAuth2_V1.1.zip


3、在登錄頁面放置QQ登錄的圖標。<img src=QQ登錄圖標文件在服務器上的地址 onclick=按鈕點擊事件>


<a href="#"onclick='toQzoneLogin()'><imgsrc="img/qq_login.png"></a>

<script>

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");

}

</script>



4、修改comm/config.php 中的4個變量

$_SESSION["appid"];

$_SESSION["appkey"];

$_SESSION["callback"];

$_SESSION["scope"];


5以下是代碼的【實現】

oauth/qq_login.php

<?php

require_once("../comm/config.php");



function qq_login($appid,$scope, $callback)

{

$_SESSION['state'] = md5(uniqid(rand(),TRUE)); //CSRF protection

$login_url="https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id="

. $appid ."&redirect_uri=" . urlencode($callback)

. "&state=" .$_SESSION['state']

. "&scope=".$scope;

header("Location:$login_url");

}


//用戶點擊qq登錄按鈕調用此函數

qq_login($_SESSION["appid"],$_SESSION["scope"], $_SESSION["callback"]);

?>


<?php

 //應用的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    

    $dialog_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id="

       . $app_id. "&redirect_uri=" . urlencode($my_url) . "&state="

       .$_SESSION['state'];

    echo("<script>top.location.href='" . $dialog_url . "'</script>");

 }


//Step2:通過Authorization Code獲取Access Token

 if($_REQUEST['state'] == $_SESSION['state'])

 {

    //拼接URL  

    $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"

    . "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 "<h3>error:</h3>" . $msg->error;

         echo "<h3>msg  :</h3>" .$msg->error_description;

         exit;

       }

    }

//Step3:使用Access Token來獲取用戶的OpenID

    $params = array();

    parse_str($response, $params);

    $graph_url = "https://graph.qq.com/oauth2.0/me?access_token="

    $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"<h3>error:</h3>" . $user->error;

       echo"<h3>msg  :</h3>" .$user->error_description;

       exit;

    }

    echo("Hello " .$user->openid);

 }

 else

 {

    echo("The state does notmatch. You may be a victim of CSRF.");

 }

?>

以上是寫好的接口,實際上我們只需要在回調頁面 callback.php修改就可以。


<?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."<br>";

$oid = $qc->get_openid();

$SESSION->oid=$oid;

//echo $SESSION->oid."<br>";

$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.'<br><ahref="'.$_SERVER['PHP_SELF'].'"></a>';

}

//add_to_log(SITEID,'auth_qq', '', '', $username . '/' . $useremail . '/' . $userid);

//$user =authenticate_user_login($username,$password);

//var_dump($user);

complete_user_login($user);


echo "<scriptlanguage=\"javascript\">


window.opener.location.reload();

window.close();

</script>";

?>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章