2013-07-14 103 views
8

我使用Facebook的JavaScript SDK在用戶單擊Login按鈕時彈出Login Popup。Facebook API:使用JavaScript SDK登錄,然後使用PHP檢查登錄狀態

的代碼,如Facebook提供了doucmentation:

$(".loginButton").click(function(){ 
FB.login(function(response) { 
    FB.api('/me', function(response) { 
     console.log(response.id); 
     //User ID shows up so I can see that the user has accepted the app. 
    }); 
}); 

我也可以用FB.getLoginStatus()來檢查用戶確實登錄並受理申請。

但是,現在我想用PHP來執行一個函數。據我所知,PHP有$user,在成功登錄後分配了UserID。

問題出在JS登錄後$user仍然是0。我堅持,我想不通爲什麼它不會正確的用戶ID分配給$user

這裏是我的JS:

<script type="text/javascript"> 

     window.fbAsyncInit = function() { 
      FB.init({ 
       appId : '<?php echo $AppId; ?>', 
       status : true, // check login status 
       cookie : true, // enable cookies to allow the server to access the session 
       xfbml : true, 
       channelUrl : '<?php echo $ServerPath; ?>channel.php' // custom channel 
      }); 

    }; 


    //Get Login Status 
    function amILoggedIn(){ 
     var toreturn = ""; 
     FB.getLoginStatus(function(response) { 
      if (response.status === 'connected') { 
       window.userId = response.authResponse.userID; 
      toreturn = "GetLoginStatus: logged in" + " " + "<?php echo $user; ?>"; 
      } else { 
      toreturn = "GetLoginStatus: Logged Out"; 
      } 
      console.log(toreturn); 
     }); 


    }; 


    // Load the SDK asynchronously 
    (function(d, s, id){ 
     var js, fjs = d.getElementsByTagName(s)[0]; 
     if (d.getElementById(id)) {return;} 
     js = d.createElement(s); js.id = id; 
     js.src = "//connect.facebook.net/en_US/all.js"; 
     fjs.parentNode.insertBefore(js, fjs); 
     }(document, 'script', 'facebook-jssdk')); 

      var obj = { 
        method: 'feed', 
        link: 'http://www.google.com' 
       }; 


    $(".loginPopupButton").click(function(){ 
     FB.login(function(response) { 
      FB.api('/me', function(response) { //user-specific stuff 
       console.log(response.id); 
      }); 
     }); 

    }); 

    </script> 

而這裏的PHP:

<?php 
    include_once('fbapi/facebook.php'); 
    include_once('fbapi/Authenticated_User.php'); 
    include_once('config.php'); 


    // Create our Application instance. 
    $facebook = new Facebook(array(
        'appId' => $AppId, 
        'secret' => $AppSecret 
       )); 
    //Facebook Authentication part 
    $user = $facebook->getUser(); 


    $perms = "manage_pages"; 
    if(isset($_GET['backlink'])) 
     $redirectUrl = urldecode($_GET['backlink']); 
    else 
     $redirectUrl = $fullserverpath; 

    $cancelUrl = $ServerPath."index.php?page=loginWFb"; 

    //AA - where to go to get FB popup.  
    $loginUrl = $facebook->getLoginUrl(
      array(
       'scope' => $perms, 
       'redirect_uri' => $redirectUrl, 
       'cancel_uri' => $cancelUrl 
      ) 
    ); 



    //AA- Defining that a powerUSER is someone who's logged in 
    if(isset($user)){ 
     $_SESSION['loggedIn'] = $user; 

    } 

    ?> 
+0

你需要通過th從JS訪問令牌到PHP(並在PHP庫中使用它)。 –

回答

4

使用JS SDK登錄將登錄數據存儲在Facebook cookie中,同時使用PHP SDK登錄將數據存儲在PHP會話中。因此,如果您使用JS SDK進行登錄,PHP SDK將不會意識到這一點。

FB.getLoginStatus(function(response) { 
    if (response.status === 'connected') { 
     // Full redirect or ajax request according to your needs 
     window.location.href = 'login.php?signed_request='+response.authResponse.signedRequest; 
    } 
}); 

然後$facebook->getUser()會給你在你的PHP的用戶ID:

爲了在使用JS SDK的數據,你需要在查詢參數簽名的請求來調用你的PHP頁面的PHP SDK登錄碼。

+2

_「因此,如果您使用JS SDK進行登錄,PHP SDK將不會意識到這一點。」_ - 官方文檔[請求不同](https://developers.facebook.com/docs/php/gettingstarted/ #整合)... – CBroe

+0

你說得對,我不知道。那麼我會提出一個更簡單的答案。 – Ale

0

當PHP初始化Facebook的對象,嘗試設置sharedSession參數true

$facebook = new Facebook(array(
    'appId'   => $AppId, 
    'secret'  => $AppSecret, 
    'sharedSession' => true 
)); 
0

你可以找到從這裏

http://liferaypower.blogspot.com/2013/10/index.html

示例代碼

FB.init({ 
    appId: '490986011016528', // App ID 

    channelUrl: 'http://localhost:8000/', // Channel File 

    status: true, // check login status 

    cookie: true, // enable cookies to allow the server to access session 

    xfbml: true // parse XFBML 

}); 

FB.Event.subscribe('auth.authResponseChange', function(response) 
{ 

    if (response.status === 'connected') 
    { 

     document.getElementById("message").innerHTML += "ConnectedtoFacebook"; 
} 
else if (response.status === 'not_authorized') 
{ 

document.getElementById("message").innerHTML += "<br>Failed to Connect"; 
    //FAILED 

} 

else 
    { 

    document.getElementById("message").innerHTML += "<br>Logged Out"; 
     //UNKNOWN ERROR 

    } 
}); 
工作示例