• php使用第三方登录


    目前只做了微博和qq的,前面的去connect.qq.com,open.weibo.com注册的步骤省略

    qq和weibo站点都有可以现在的php版本的api,qq的api相对高大上一些。

    <script>
    function toLogin(who)
    {
         //以下为按钮点击事件的逻辑。注意这里要重新打开窗口
         //否则后面跳转到QQ登录,授权页面时会直接缩小当前浏览器的窗口,而不是打开新窗口
       //"/dlt/index.php?wherego="+who+"&back_url=$url",这里的$url需要使用urlencode,js的话要用escape,encodeUrl函数
      //嵌套在iframe里时要用window.top.open var A=window.open("/dlt/index.php?wherego="+who,"OauthLogin","width=450,height=320,menubar=0,scrollbars=1,resizable=1,status=1,titlebar=0,toolbar=0,location=1"); } </script> <li class="dl_tips"> <a href="javascript:void(0);" onclick="toLogin('qq');"><cite><img src="<?=static_path('images/login/qq.png')?>" /></cite><cite>腾讯QQ登录</cite></a> <a href="javascript:void(0);" onclick="toLogin('wb');"><cite><img src="<?=static_path('images/login/wb.png')?>" /></cite><cite>新浪微博登录</cite></a> </li>

     整个实现登录的流程在一个独立的包里结构如下

    /dlt---根目录
    /dlt/index.php---入口地址
    /dlt/callback.php--回调地址
    
    /dlt/lib---自定类,配置义目录
    /dlt/lib/dlt_client.php---流程入口
    /dlt/lib/db_client.php
    /dlt/lib/model.php
    
    
    /dlt/sdk---第三方sdk目录
    /dlt/qq/API---qq的sdk目录,目录详情省略
    /dlt/wb/API---weibo的sdk目录,目录详情省略

    /dlt/var---日志记录目录

     入口地址

    <?php
    $wherego = $_GET['wherego'];
    if(!empty($wherego) && in_array( $wherego,array('qq','wb'))){
    	require 'lib/dlt_client.php';
    	$dlt_client = new DLT($wherego);
    	$dlt_client->save_referer();
    	$dlt_client->go();
    }else{
    	exit('传入参数错误');
    }
    

     回调地址

    <?php
    $whereback = $_GET['whereback']; if(!empty($whereback) && in_array( $whereback,array('qq','wb'))){ require 'lib/dlt_client.php'; $dlt_client = new DLT($whereback); $dlt_client->callback(); }else{ exit('回传参数错误'); }

     流程入口文件

    <?php
    class DLT {
    	public $who;
    	public $db;
    	public $user_model;
    	public $e_logo_prefix = 'http://user.X.com/static/';
    	private $_sdk_path;
    	private $_dlt_back_url;
    	
    	public function __construct($who){
    		$this->who = $who;
    		$this->_sdk_path = dirname(dirname(__FILE__)) . '/sdk/';
    	}
    	
    	public function save_referer(){
    		if(!empty($_GET['back_url'])){
    			setcookie ('dlt_back', $_GET['back_url'], time()+60*5, '/', 'X.com');
    		}else if(!empty($_SERVER['HTTP_REFERER'])){
    			setcookie ('dlt_back', $_SERVER['HTTP_REFERER'], time()+60*5, '/', 'X.com');
    		}else{
    			return false;
    		}
    	}
    	
    	public function go(){
    		$function_name = 'go_' . $this->who;
    		$this->$function_name();
    	}
    	
    	public function callback(){
    		$this->_dlt_back_url = $_COOKIE['dlt_back'];
    		if(empty($this->_dlt_back_url)){
    			$this->_dlt_back_url = 'http://user.X.com';
    		}
    		$function_name = 'callback_' . $this->who;
    		$this->$function_name();
    	}
    	
    	public function go_qq(){
    		require_once($this->_sdk_path . "qq/API/qqConnectAPI.php");
    		$qc = new QC();
    		$qc->qq_login();
    	}
    	
    	public function callback_qq(){
    		require_once($this->_sdk_path . "qq/API/qqConnectAPI.php");
    		$qc = new QC();
    		$access_token = $qc->qq_callback();
    		$openid = $qc->get_openid();
    		//var_dump($access_token);
    		//var_dump($openid);
    		if(empty($openid) || empty($access_token)){
    			exit('get QQ token error!');
    		}
    		$qc = new QC($access_token,$openid);
    		$arr = $qc->get_user_info();
    		//var_dump($arr);exit;
    		if(!isset($arr['nickname'])){
    			exit('get QQ userinfo error!');
    		}
    		$user_info['m_o_name'] = $arr["nickname"];
    		$user_info['m_dlt_type'] = 'qq';
    		$user_info['m_dlt_id'] = $openid;
    
    		$this->do_login($user_info);
    	}
    	
    	public function go_wb(){
    		require_once($this->_sdk_path . "wb/API/wbConnectAPI.php");
    		$wbc = new WBC();
    		$wbc->wb_login();
    	}
    	
    	public function callback_wb(){
    		require_once($this->_sdk_path . "wb/API/wbConnectAPI.php");
    		$wbc = new WBC();
    		$access_token = $wbc->get_token();
    		$uid = $wbc->get_uid();
    		//var_dump($access_token);
    		//var_dump($uid);
    		if(empty($uid) || empty($access_token)){
    			exit('get WB token error!');
    		}
    		$arr = $wbc->get_user_info();
    		//var_dump($arr['name']);exit;
    		if(!isset($arr['name'])){
    			exit('get WB userinfo error!');
    		}
    		$user_info['m_o_name'] = $arr["name"];
    		$user_info['m_dlt_type'] = 'wb';
    		$user_info['m_dlt_id'] = $uid;
    
    		$this->do_login($user_info);
    	}
    	
    	public function do_login($user_info){
    		require 'model.php';
        		$this->model = new Model();
              //没有就插入,有就返回唯一id $member_info = $this->model->check_member($user_info); if(empty($member_info)){ exit('db operate error!'); }
              //回写获取的昵称 $member_info['m_o_name'] = $user_info['m_o_name']; $this->init_session($member_info); } public function init_session($member_info){ $data = $this->model->get_all($member_info['m_id']); $data['member_id'] = $data['m_id']; //会员id $data['member_name'] = $data ['m_o_name'] = $member_info['m_o_name']; //会员名 $data['member_email'] = $data ['m_email']; //会员注册邮箱 $data['contact_name'] = $data['mp_name']; //会员名 $data['erp_source'] = $data ['m_source']; //会员来源地 $data['member_identity'] = 1; //会员身份 foreach($data as $key => $value){ if(!in_array($key,$sessionArr)){ unset($data[$key]); } } //因为两者的api中都是用了session_start,所以要先销毁掉。 session_destroy(); $config["lifetime"]=0; $config["path"] = '/'; $config["domain"] = '.X.com'; $config["secure"]=false; $config["httponly"] =false; session_set_cookie_params( $config["lifetime"], $config["path"], $config["domain"], $config["secure"], $config["httponly"] ); session_name('mphsess_id'); session_start();    foreach($data as $key => $val){ $_SESSION[$key] = $val; } $this->dlt_back(); } //这里的window.opener.location.reload()有兼容性问题,可能还有跨域的问题,我在news站点使用,user站点的窗口不能调用父窗口news的reload方法。
        //window.close()也有浏览器兼容性问题,有的需要添加window.opener=null public function dlt_back(){ //header("Location: {$this->_dlt_back_url}"); echo <<<EOT <script language="javascript" type="text/javascript"> window.opener.location='{$this->_dlt_back_url}'; window.close(); </script> EOT; exit; } }
  • 相关阅读:
    强化学习
    详解a标签中href=""的几种用法 锚点
    使用Django自带的登录访问限制login_required
    ValueError: invalid literal for int() with base 10: ''
    Django 中创建Model时报以下错误: TypeError: init() missing 1 required positional argument: ‘on_delete’
    bootstrp实现同一界面多个模态框
    第十二章 Django框架开发
    Python format 格式化函数
    Python3.x在django中如何设置Content-Disposition,才能让浏览器正确保存中文命名的文件?
    python中的*和**参数传递机制
  • 原文地址:https://www.cnblogs.com/kudosharry/p/3643436.html
Copyright © 2020-2023  润新知