ecshop之整合QQ登陆

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}

完毕。

备注:
用户中心的登陆者昵称可以根据步骤三来修改,建议不用改,因为就算这里改了后台即时注册的用户名具了解出于隐私保护依旧是不变的长代码,由于可能还要集成其他的快捷登录,戏子觉得没太大必要尽量不要改动太多,免得版本升级麻烦,我也是偷懒…

 

转载于:https://www.cnblogs.com/lonelystarxing/archive/2012/03/02/2376874.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值