2011-04-12 70 views
0

我想讓用戶使用他們的Facebook帳戶登錄到我的網站。我曾嘗試從Facebook開發者頁面(http://developers.facebook.com/docs/)閱讀與該過程相關的一些信息 - 雖然過程和邏輯類型有意義,但我仍然忽略了大局。Facebook連接的網站 - 數據庫連接和邏輯

'Facebook連接'如何鏈接到我現有的用戶及其屬性數據庫。假設有一個叫John Walker的用戶,他是一個註冊用戶(登錄名:jwalker pass:1234賬戶餘額:10美元)。當他來到網站,並嘗試使用Facebook登錄進行登錄時,該如何知道在哪裏指導他?換句話說,Facebook如何知道它是正確的John Walker,並且他應該被髮送到他的用戶頁面?

如果這個問題微不足道,我很抱歉,但我不能認爲這是我自己。感謝您的幫助!

扎卡里

回答

0

好扎卡里,您對Facebook的連接有點誤解。當用戶點擊放置在您網站上的Facebook連接按鈕時,Facebook不會訪問您的數據庫來驗證用戶身份。他在他的數據庫中驗證用戶是否當前用戶有Facebook帳戶...如果他/她有帳戶,那麼Facebook通過將他登錄到Facebook來爲該特定用戶頒發訪問令牌。那麼現在您有責任在系統中登錄該用戶,並將他的信息放入您的數據庫。讓我進一步解釋並看看下面的代碼

 
<div id="fb-root"></div> 
<script> 
    window.fbAsyncInit = function() { 
    FB.init({appId: "Your APP ID", 
      status: true, 
      cookie: true, 
      xfbml: true}); 

    FB.getLoginStatus(function(response) { 
     if (response.session) { 
     // This is the place where you get control when user is login to facebook 
       // At this point you can redirect the user to a page where you can put the facebook returned information to database and login him into your system 

     } else { 

     } 
    }); 
    FB.Event.subscribe("auth.login", function(response) { 

    window.location.reload(); 
     }); 
FB.Event.subscribe("auth.logout", function(response) { 


    self.location.href="http://www.iranibash.com/logout.php"; 
     }); 
    }; 
    (function() { 
    var e = document.createElement("script"); 
    e.src = document.location.protocol + "//connect.facebook.net/en_US/all.js"; 
    e.async = true; 
    document.getElementById("fb-root").appendChild(e); 
    }()); 
    </script> 

現在您可以使用Facebook成功登錄後返回您的用戶信息。您可以在$ cookie中獲得該信息。我認爲你可以從這些信息中瞭解嗎?