團購實現oauth第三方登入

來源:互聯網
上載者:User

接過一個簡單的實現oauth登入的小單子,使用2345帳號登入最土的團購程式,憑著自己半天的開發新浪微博應用的經驗開始了這個oauth的實現,下面是實現的源碼,這是2345給的api下的callback.php檔案

<?php<br />/*<br /> * 使用者認證後,獲得的access_token以及使用者資訊<br /> */<br />header("Content-Type: text/html;charset=utf-8");<br />session_start();<br />include_once('lib/Tuan2345Auth.php');<br />//0331修改<br />require_once("./lib/config.php");<br />$h = new Tuan2345Auth($_SESSION['request_token']['oauth_token'],$_SESSION['request_token']['oauth_token_secret']);<br />$accessToken = $h->getAccessToken($_REQUEST['oauth_verifier']);<br />$oauth_token = $accessToken["oauth_token"];<br />$oauth_secret = $accessToken["oauth_token_secret"];<br />$uid = $accessToken["user_id"];<br />include_once("lib/Tuan2345Client.php");<br />$client = new Tuan2345Client();<br />$userinfo = $client->get_userinfo($uid);<br />if($userinfo){<br />$userinfo_arr = explode("|",$userinfo);<br />}<br />$_SESSION["access_token"] =array('oauth_token'=>$oauth_token,'oauth_token_secret'=>$oauth_secret);<br />$errorno = $accessToken["error_no"];<br />if(!empty($errorno))<br />{<br /> //可以對應跳轉到錯誤頁面<br /> echo " AUTH FAIL !";<br /> echo $accessToken["msg"];<br />}<br />if(!empty($oauth_token) && !empty($oauth_secret))<br />{<br />header('Location: ./thirdpart/2345/deal_2345login.php');<br /> /*<br /> echo "access_token=".$accessToken["oauth_token"]."<br>";<br /> echo "access_token_secret=".$accessToken["oauth_token_secret"]."<br>";<br /> echo "qid=".$userinfo_arr[0]."<br>";<br /> echo "qname=".$userinfo_arr[1]."<br>";<br /> echo "qmail=".$userinfo_arr[2]."<br>";*/<br /> $sns = "2345:".$userinfo_arr[0];<br />$exist_user = Table::Fetch('user', $sns, 'sns'); // user是資料表 $sns 是值 'sns'是欄位 返回的結果應該是全部的資料<br />//如果使用者存在則設定session的值並且跳轉到index.php首頁<br />if ( $exist_user ) {<br />Session::Set('user_id', $exist_user['id']);<br />//exit();<br />Utility::Redirect(get_loginpage(WEB_ROOT . '/index.php'));//重新導向到首頁<br />}</p><p>$prompt_name = $userinfo_arr[1];<br />$exist_user = Table::Fetch('user', $prompt_name, 'username');//取出username為$promopt_name 對應的值的資料記錄<br />while(!empty($exist_user)) {<br />$prompt_name = $ms['screen_name'] .'_' . rand(10000,99999);<br />$exist_user = Table::Fetch('user', $prompt_name, 'username');<br />}</p><p>$new_user = array(<br />'username' => $userinfo_arr[1],<br />'realname' => $userinfo_arr[1],<br />'email' => $userinfo_arr[2],<br />'password' => rand(10000000,99999999),<br />'gender' => 'M',<br />'sns' => $sns,<br />);<br />//將使用者資料寫入資料庫中<br />if ( $user_id = ZUser::Create($new_user, true) ) {<br />Session::Set('user_id', $user_id);<br />Utility::Redirect(get_loginpage(WEB_ROOT . '/index.php'));<br />}</p><p>Utility::Redirect(WEB_ROOT . '/thirdpart/sina/login.php' );<br />}<br />echo '<br><a href="index.html" mce_href="index.html" >再來一次</a>';<br />?> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.