日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php使用第三方登录

發布時間:2025/7/14 php 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.openvar 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);" οnclick="toLogin('qq');"><cite><img src="<?=static_path('images/login/qq.png')?>" /></cite><cite>騰訊QQ登錄</cite></a> <a href="javascript:void(0);" οnclick="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=nullpublic 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;} }

?

轉載于:https://www.cnblogs.com/kudosharry/p/3643436.html

總結

以上是生活随笔為你收集整理的php使用第三方登录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。