PHPMPS/xookee 添加QQ快捷登录的办法

原文出自:http://blog.sina.com.cn/s/blog_0ceb8b6b0102vp0k.html 在作者的帮助下略有修改

登陆 connect.qq.com 申请网站接入获取 APPID和APPKET  http://connect.qq.com/

回调地址为:http://你的域名/qqreg.php

好了,直接进入正题

首先在phpmps_member表里添加:access_token和openid两个字段,都是varchar,50字节。

ALTER TABLE `phpmps_member` ADD `access_token` CHAR(50) NOT NULL ;
ALTER TABLE `phpmps_member` ADD `openid` CHAR(50) NOT NULL ;

添加方法参考  ALTER TABLE  `pre_common_member_connect` ADD  `conuintoken` CHAR(50) NOT NULL ;

1、在include目录下添加qqinc.php文件,内容为

<?php

$usersite= $_SERVER[‘HTTP_HOST’];

$appid=’APPID’;   //修改为你申请的QQ快捷登录APPID。

$appkey=’APPKEY’;   //修改为你申请的QQ快捷登录APPKEY.

$redirect_uri=”http://”.$usersite.”/qqreg.php”;

$redirect_uri=urlencode($redirect_uri);

class http{

function post($url , $params_array = array(), $header = array()){

$ch = curl_init(); // 初始化CURL句柄

curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL

curl_setopt($ch, CURLOPT_POST, 1); //启用POST提交

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // 设为TRUE把curl_exec()结果转化为字串,而不是直接输出

$postdata = ”; //请求参数数组转化为以’&’分隔的字符串

if(!empty($params_array)) {

foreach($params_array as $k=>$v) {

$postdata .= $k.’=’.rawurlencode($v).’&’; //注意,此处统一对传入参数做urlencode处理,请勿重复encdoe参数

}

$postdata = substr($postdata, 0, -1);

}

curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); //设置POST提交的请求参数

curl_setopt($ch,CURLOPT_HTTPHEADER,$header); //设置HTTP头信息

curl_setopt($ch, CURLOPT_TIMEOUT, 15); //设置超时时间15秒

$response = curl_exec($ch); //执行预定义的CURL

curl_close($ch); //关闭CURL

return $response;

}

function get($url){

$ch = curl_init(); // 初始化CURL句柄

curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // 设为TRUE把curl_exec()结果转化为字串,而不是直接输出

curl_setopt($ch, CURLOPT_TIMEOUT, 15); //设置超时时间15秒

$response = curl_exec($ch); //执行预定义的CURL

curl_close($ch); //关闭CURL

return $response;

}

}

?>

2、在include/common.php最后一行前加入“require PHPMPS_ROOT . ‘include/qqinc.php’;”

即末尾变成:

require PHPMPS_ROOT . ‘include/qqinc.php’;
require PHPMPS_ROOT . ‘include/header.php’;

3、添加QQ登陆加按钮

在templates\phpmps\header.htm里

找到<font color=”red”><a href=”member.php?act=login&refer={$PHP_URL}”>

在前面插入

<a href=”https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id={$appid}&redirect_uri={$redirect_uri}&state={php $state= md5(uniqid(rand(), TRUE));$_SESSION[‘state’]=$state;echo $state;}” target=_blan
k><img src=”templates/{$CFG[tplname]}/images/Connect_logo_3.png” width=”120″ height=”24″ alt=”QQ登陆” align=”right”/></a>

img src=”templates/{$CFG[tplname]}/images/Connect_logo_3.png”  //QQ登陆图片路径自行修改

4、在根目录下添加qqreg.php文件,内容为:

<?php

define(‘IN_PHPMPS’, true);

if(empty($_GET[‘code’]) || !isset($_GET[‘code’]) || $_GET[‘code’]==” || $_GET[‘code’]==null){

}

else

{

require dirname(__FILE__) . ‘/include/common.php’;

$url=”https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=”.$appid.”&client_secret=”.$appkey.”&code=”.$_GET[‘code’].”&redirect_uri=”.$redirect_uri;
$http = new http();

$string=$http->get($url);

$num= strpos($string,’token=’)+6;

$rnum=strrpos($string,’&expires’);

$access_token=substr($string,$num,$rnum-$num);

$num= strpos($string,’state=’)+6;

$state=substr($string,$num);

if (!$_SESSION[‘state’]==$state)

{echo “”;exit();}

$url=”https://graph.qq.com/oauth2.0/me?access_token=”.$access_token;

$string=$http->get($url);

$num= strpos($string,’openid”:”‘)+9;

$rnum=strrpos($string,'”‘);

$openid=substr($string,$num,$rnum-$num);

$url=”https://graph.qq.com/user/get_user_info?access_token=”.$access_token.”&oauth_consumer_key=”.$appid.”&openid=”.$openid;

$string=$http->get($url);//QQ用户详细资料

$qqjiexi=json_decode($string);

$sql =”select username from {$table}member where openid=’$openid'”;

$username=$db->getOne($sql);

if ($username)//如果有此会员,则登录主界面。

{

set_session ($username);

header(“Location: index.php”);

}

else//如果没有此会员,则绑定或注册新会员。

{

$_SESSION[‘access_token’]=$access_token;

$_SESSION[‘openid’]=$openid;

$_SESSION[‘qqname’]=$qqjiexi->nickname;

header(“Location: member.php?act=register&qq=t”);

}

}

?>

5、修改templates\phpmps\register.htm

在<div class=”regc_1 clearfix”>后插入

<input type=”hidden” name=”access_token” value=”{$access_token}”>
<input type=”hidden” name=”openid” value=”{$openid}”>

6、根目录下的member.php文件

if($CFG[‘close_register’] == ‘1’) showmsg(‘本站已关闭用户注册。’); 前插入

 

$qqname=””;
$access_token=””;
$openid=””;
$seo[‘title’] = “会员注册”;
if ($_REQUEST[‘qq’]===”t”)
{
$access_token=$_SESSION[‘access_token’];
$openid=$_SESSION[‘openid’];
$seo[‘title’] = “QQ会员快捷登录注册”;
$qqname=$_SESSION[‘qqname’];
if (!empty($qqname))
{
$sql =”select username from {$table}member where username=’$qqname'”;
$username=$db->getOne($sql);
if ($username){$qqname=$_SESSION[‘qqname’].date(“Ymdhis”);}
}
}

if(empty($username))showmsg(“用户名不能为空”); 前插入

$access_token=$_POST[‘access_token’];
$openid=$_POST[‘openid’];

然后:

$sql = “insert into {$table}member (uid,username,email,password,registertime,registerip,lastlogintime) values (‘$uid’,’$username’,’$email’,’$md5_password’,’$regtime’,’$ip’,’$lastlogintime’)”;

 

修改为:

$sql = “insert into {$table}member (uid,access_token,openid,username,email,password,registertime,registerip,lastlogintime) values (‘$uid’,’$access_token’,’$openid’,’$username’,’$email’,’$md5_password’,’$regtime’,’$ip’,’$lastlogintime’)”;

 

接着

if(register($username,$md5_password,$email))

修改为

if(register($username,$md5_password,$email,$access_token,$openid)) {

7、include\global.fun.php

function register($username, $password, $email)

改为

function register($username, $password, $email,$access_token=”,$openid=”)

 

$sql = “INSERT INTO {$table}member (username,password,email,registertime,registerip,lastlogintime,status) VALUES (‘$username’,’$password’,’$email’,’$time’,’$ip’,’$time’, ‘$status’)”;

改为

$sql = “INSERT INTO {$table}member (access_token,openid,username,password,email,registertime,registerip,lastlogintime,status) VALUES (‘$access_token’,’$openid’,’$username’,’$password’,’$email’,’$time’,’$ip’,’$time’, ‘$status’)”;

 

至此就大功告成了,这只是一个大致的方案.可以把添加APPID和APPKEY这一步修改到管理后台去操作