2016-10-20 217 views
0

我在使用PHP SDK的Facebook登錄時遇到了問題。Facebook登錄在第一次嘗試獲取會話時失敗

我有if else語句的會話變量,因爲我也使用這個登錄名將用戶信息存儲到db,比如名字,電子郵件和uid。

問題我第一次登錄時,代碼將始終爲執行腳本的會話執行ELSE語句,該腳本顯示「請再次嘗試登錄」,然後當用戶嘗試再次登錄時,它會成功的。我需要幫助解決這個問題,以便在第一次嘗試時識別用戶。

href="example.org/fb-redirect.php" 

感謝:

而且,我的登錄按鈕被通過訪問!

<?php 

include('debug.php'); 
require_once(dirname(__FILE__)."/templates/initialize.inc"); 

session_start(); 
$curtimestamp = date('Y-m-d H:i:s'); // added in v4.0.0 
require_once 'fb/autoload.php';  
require_once('fb/src/Facebook/GraphUser.php'); 
use Facebook\FacebookSession; 
use Facebook\FacebookRedirectLoginHelper; 
use Facebook\FacebookRequest; use Facebook\FacebookResponse; 
use Facebook\FacebookSDKException; 
use Facebook\FacebookRequestException; 
use Facebook\FacebookAuthorizationException; 
use Facebook\GraphObject; 
use Facebook\GraphUser; 
use Facebook\Entities\AccessToken; 
use Facebook\HttpClients\FacebookCurlHttpClient; 
use Facebook\HttpClients\FacebookHttpable; 

function authoLogin($userLogin){  
$global_connection = db_connect(); 
$userdata = db_getrow("select * from user where user_login like '".$userLogin."'", $global_connection);  
db_query("update user set user_loginerror=0 where user_id='".$userdata['user_id']."'", $global_connection);  
util_setsessionvar('usertype',$userdata['usertype_id']);  
util_setsessionvar('userid',$userdata['user_id']); 
util_setsessionvar('userlogin',$userdata['user_login']);  
util_setsessionvar('username',$userdata['user_name']); 
} 
// init app with app id and secret 
FacebookSession::setDefaultApplication('appid','secret'); 
// login helper with redirect_uri 

$helper = new FacebookRedirectLoginHelper('http://example.org/fb-redirect.php'); 
if (isset($_SESSION) && isset($_SESSION['fb_token'])) { 
// create new session from saved access_token 
$session = new FacebookSession($_SESSION['fb_token']); 

// validate the access_token to make sure it's still valid 
try { 
    if (!$session->validate()) { 
     $session = null; 
    } 
} catch (Exception $e) { 
    // catch any exceptions 
    $session = null; 
    } 
} 
if (!isset($session) || $session === null) { 
// no session exists 
    try { 
    $session = $helper->getSessionFromRedirect(); 
} catch(FacebookRequestException $ex) { 
    // When Facebook returns an error 
    // handle this better in production code 
    print_r($ex); 
} catch(Exception $ex) { 
    // When validation fails or other local issues 
    // handle this better in production code 
    print_r($ex); 
    }  

} 
// see if we have a session 
// see if we have a session 
if (isset($session)) { 
//store the token in the php session 
// graph api request for user data 
$request = new FacebookRequest($session, 'GET', '/me'); 
$response = $request->execute(); 
// get response 
$graphObject = $response->getGraphObject(GraphUser::classname()); 
     $fbid = $graphObject->getProperty('id');    // To Get Facebook ID 
     $fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name 
     $femail = $graphObject->getProperty('email'); // To Get Facebook email ID   $fgetid=$graphObject->getID();  //get image source from facebook account 
     $getimgsrc='https://graph.facebook.com/'.$fgetid.'/picture?width=200'; 

     /* ---- Session Variables -----*/ 
     $_SESSION['FBID'] = $fbid;   
     $_SESSION['FULLNAME'] = $fbfullname; 
     $_SESSION['EMAIL'] = $femail;  $_SESSION['PROFILE_PICTURE']=$getimgsrc;  $_SESSION ['form_data']['return_url'] = $_SERVER['HTTP_REFERRER']; 


    if statement { 
     //save to database 
     } else if statement{  
} 
    /* ---- header location after session ----*/ 

}else{ 
echo "<script> alert('Please try to login again'); window.location.href='index.php'; </script>"; 
} 
+0

我猜首先嚐試沒有變量$ session集。但在重新加載之前,$ session被設置爲null。現在它會被你的if語句在下次重新載入時拾取,而不是第一次嘗試。 –

+0

我無法弄清楚自用戶剛剛做了同樣的事情後,如何才能讓它第一次識別,只需用fb按鈕單擊登錄即可。 @LarsMertens – Cisco1991

+0

不確定在設置$ session之前你需要檢查。 if(isset($ _SESSION)&& isset($ _SESSION ['fb_token'])) –

回答

0

現在可以關閉了。通過使用Facebook的PHP SDK的最新版本修復了一切。謝謝!

相關問題