1.
首先去QQ網站申請一個 APP KEY 和 APPID ,申請地址為 http://connect.opensns.qq.com/apply
2.
下載本站提供的QQ登錄插件,保存到你網站根目錄即可。
打開你下載到的 QQ.PHP 文件,
修改 下面三個地方
‘oauth_c**umer_key’=>’******’, // 這里輸入在QQ網站申請到的APP ID
‘oauth_c**umer_secret’=>’**********’, //這里輸入在QQ網站申請到的APP KEY
‘oauth_callback’=>”http://www.xxxxx.cn/qq.php?action=reg”, //這里要把 www.xxxx.cn 修改為你的真實域名
3.
直接使用 http://你的域名/qq.php?action=login 進行登錄,或者你在頭文件里放一個連接指向 /qq.php?action=login
qq.php文件
<?php
/****
* 作者:落幕的戲子(發布) lonelystarxing(修改)
* 申請APPID的地址: http://connect.opensns.qq.com/apply
*/
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . 'includes/lib_transaction.php');
include_once(ROOT_PATH . 'includes/lib_passport.php');
function check_user($username){
$sql = "SELECT user_id FROM " . $GLOBALS['ecs']->table("users"). " WHERE user_name='$username'";
$row = $GLOBALS['db']->getRow($sql);
if (!empty($row)){ return true; }else{return false;}
}
$qq_oauth_config = array(
'oauth_consumer_key'=>'****', // 輸入在QQ網站申請到的APP ID
'oauth_consumer_secret'=>'****', //輸入在QQ網站申請到的APP KEY
'oauth_callback'=>"http://www.****.cn/qq.php?action=reg", //www.****.cn 修改為你的域名
'oauth_request_token_url'=>"http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token",
'oauth_authorize_url'=>'http://openapi.qzone.qq.com/oauth/qzoneoauth_authorize',
'oauth_request_access_token_url'=>'http://openapi.qzone.qq.com/oauth/qzoneoauth_access_token',
'user_info_url' => 'http://openapi.qzone.qq.com/user/get_user_info',
);
$action = isset($_GET['action']) ? $_GET['action'] : '';
$qq = new qq_oauth($qq_oauth_config);
switch($action){
//用戶登錄 Step1:請求臨時token
case 'login':
$token = $qq->oauth_request_token();
$_SESSION['oauth_token_secret'] = $token['oauth_token_secret'];
$qq->authorize($token['oauth_token']);
break;
//Step4:Qzone引導用戶跳轉到第三方應用
case 'reg':
$qq->register_user();
$access_token = $qq->request_access_token();
if($token = $qq->save_access_token($access_token)){
$_SESSION['oauth_token'] = $token['oauth_token'];
$_SESSION['oauth_token_secret'] = $token['oauth_token_secret'];
$_SESSION['openid'] = $token['openid'];
header('Content-Type: text/html; charset=utf-8');
$user_info = json_decode($qq->get_user_info());
$nickname = $user_info->nickname; //返回QQ昵稱
if($user_info->ret!=0){
exit("登錄發生錯誤".$user_info->msg);
} else {
$username='qq'.$_SESSION['openid'];
$password=time();//隨便弄個密碼
$email=$_SESSION['openid'].'@qq.com';//沒有返回郵箱
$back_act ="user.php";
/* 檢測用戶名 */
if (check_user($username)!==false){//賬號存在直接完成登錄
$GLOBALS['user']->set_session($username);
$GLOBALS['user']->set_cookie($username);
header("Location: user.php\n");
exit;
}else{//賬號不存在就完成注冊並自動登錄
$reg_date = time();
$password =md5($password);
$GLOBALS['db']->query('INSERT INTO ' . $GLOBALS['ecs']->table("users") . "(`email`, `user_name`, `password`, `reg_time`, `last_login`, `last_ip`) VALUES ('$email', '$username', '$password', '$reg_date', '$reg_date', '$ip')");//賬號不存在 就寫入數據庫 並登陸
$GLOBALS['user']->set_session($username);
$GLOBALS['user']->set_cookie($username);
header("Location: user.php\n");
exit;
}
//$user_info->figureurl'
}
}
break;
default :
}
class qq_oauth{
private $config;
function __construct($config){
$this->config = $config;
}
function C($name){
return isset($this->config[$name]) ? $this->config[$name] : FALSE;
}
function build_request_uri($url,$params=array(),$oauth_token_secret=''){
$oauth_consumer_key = $this->C('oauth_consumer_key');
$oauth_consumer_secret = $this->C('oauth_consumer_secret');
$params = array_merge(array(
'oauth_version'=>'1.0',
'oauth_signature_method'=>'HMAC-SHA1',
'oauth_timestamp'=>time(),
'oauth_nonce'=>rand(1000,99999999),
'oauth_consumer_key'=>$oauth_consumer_key,
),$params);
$encode_params = $params;
ksort($encode_params);
$oauth_signature = 'GET&'.urlencode($url).'&'.urlencode(http_build_query($encode_params));
$oauth_signature = base64_encode(hash_hmac('sha1',$oauth_signature,$oauth_consumer_secret.'&'.$oauth_token_secret,true));
$params['oauth_signature'] = $oauth_signature;
return $url.'?'.http_build_query($params);
}
function check_callback(){
if(isset($_GET['oauth_token']))
if(isset($_GET['openid']))
if(isset($_GET['oauth_signature']))
if(isset($_GET['timestamp']))
if(isset($_GET['oauth_vericode']))
return true;
return false;
}
function get_contents($url){
$curl = curl_init();
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_URL,$url);
return curl_exec($curl);
}
function oauth_request_token(){
$url = $this->build_request_uri($this->C('oauth_request_token_url'));
$tmp_oauth_token = $this->get_contents($url);
parse_str($tmp_oauth_token);
if(isset($error_code)) exit($error_code);
return array(
'oauth_token'=>$oauth_token,
'oauth_token_secret'=>$oauth_token_secret
);
}
function authorize($oauth_token){
$str = "HTTP/1.1 302 Found";
header($str);
$url = $this->C('oauth_authorize_url');
$query_strings = http_build_query(array(
'oauth_consumer_key'=>$this->C('oauth_consumer_key'),
'oauth_token'=>$oauth_token,
'oauth_callback'=>$this->C('oauth_callback'),
));
header('Location: '.$url.'?'.$query_strings);
}
function register_user(){
if($this->check_callback()){
//校驗簽名
$signature = base64_encode(hash_hmac('sha1',$_GET['openid'].$_GET['timestamp'],$this->C('oauth_consumer_secret'),true));
if(!empty($_GET['oauth_signature']) && $signature==$_GET['oauth_signature']){
$_SESSION['oauth_token'] = $_GET['oauth_token'];
$_SESSION['oauth_vericode'] = $_GET['oauth_vericode'];
return;
}
}
//校驗未通過
exit('UNKNOW REQUEST');
}
function request_access_token(){
$url = $this->build_request_uri($this->C('oauth_request_access_token_url'),array(
'oauth_token'=>$_SESSION['oauth_token'],
'oauth_vericode'=>$_SESSION['oauth_vericode']
),$_SESSION['oauth_token_secret']);
return $this->get_contents($url);
}
function save_access_token($access_token_str){
parse_str($access_token_str,$access_token_arr);
if(isset($access_token_arr['error_code'])){
return FALSE;
} else {
return $access_token_arr;
}
}
function get_user_info(){
$url = $this->build_request_uri($this->C('user_info_url'),array(
'oauth_token'=>$_SESSION['oauth_token'],
'openid'=>$_SESSION['openid'],
),$_SESSION['oauth_token_secret']);
return $this->get_contents($url);
}
}
?>
<a href="?action=login">使用QQ登錄網站</a>
以上內容已確定可行。只是登陸后顯示效果為qq2149C0692657C41D28A38465D9342FEE
戲子提供以下美化(待驗證,成功后標記)
相關美化:
在商城頭部顯示登陸者QQ昵稱而非類似 qq2149C0692657C41D28A38465D9342FEE 的代碼
1)、
進入ec后台 點擊 “sql查詢” 執行
ALTER TABLE `ecs_users` ADD `nick_name` VARCHAR( 100 ) NOT NULL ;
建立昵稱字段。
注:這里要注意你的表前綴是不是ecs_, 不是請自行修改
2)、
打開include下lib_main.php 找到 get_user_info 這個函數找到
$sql = ‘SELECT u.user_id, u.email, u.user_name, u.user_money, u.pay_points’.
‘ FROM ‘ .$GLOBALS['ecs']->table(‘users’). ‘ AS u ‘ .
” WHERE u.user_id = ‘$id’”;
替換為
$sql = ‘SELECT u.user_id, u.email, u.user_name,u.nick_name, u.user_money, u.pay_points’.
‘ FROM ‘ .$GLOBALS['ecs']->table(‘users’). ‘ AS u ‘ .
” WHERE u.user_id = ‘$id’”;
3)、
打開模板文件夾里的member_info.lbi 找到{$user_info.username} 修改為 {if $user_info.nick_name}{$user_info.nick_name}{else}{$user_info.username}{/if}
完畢。
備注:
用戶中心的登陸者昵稱可以根據步驟三來修改,建議不用改,因為就算這里改了后台即時注冊的用戶名具了解出於隱私保護依舊是不變的長代碼,由於可能還要集成其他的快捷登錄,戲子覺得沒太大必要盡量不要改動太多,免得版本升級麻煩,我也是偷懶…
