簡體   English   中英

Facebook身份驗證到我的PHP頁面

[英]Facebook Authentication to my PHP Page

我試圖允許用戶使用Facebook在我的頁面上注冊。 這就是我連接/登錄Facebook的方式(只是JS代碼)。

function statusChangeCallback(response) {
    if (response.status === 'connected') {
        // Logged into your app and Facebook.

        FB.api('/me', function(response) {
            var reg_Name = response.first_name;
            var reg_Surname = response.last_name;
            var reg_Email = response.email;

            //$.post("System/External.php?function=login_facebook")
        });

        console.log(response);
    } else if (response.status === 'not_authorized') {
        // The person is logged into Facebook, but not your app.
        alert('Please log into this application to continue.');
    } else {
        // The person is not logged into Facebook, so we're not sure if
        // they are logged into this app or not.
        alert('Please log into Facebook to continue.');
    }
}

function checkLoginState() {
    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
    });
}

window.fbAsyncInit = function() {
    FB.init({
        appId      : 'app_key',
        cookie     : true,  // enable cookies to allow the server to access
        // the session
        xfbml      : true,  // parse social plugins on this page
        version    : 'v2.0' // use version 2.0
    });

    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
    });

};

// 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/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

我的問題是我要在注冊后登錄。 我將使用$ .post請求進行注冊,但是由於您無法檢索用戶FB密碼,因此現在是驗證實際登錄名的最佳方法是什么?

當用戶登錄Facebook時,我希望頁面執行實際的登錄身份驗證(來自我的網站)。 例:

有兩種方法可以登錄到我的網站。

  1. 使用我的登錄界面(用戶名和密碼正確時,這將登錄您)。
  2. 使用Facebook(然后將數據發布到與通過我的網站登錄相同的PHP頁面上-就像用戶通過我的網站登錄一樣)。

我的第一個嘗試是使用“個人電子郵件”登錄到該網站(但是,如果有人獲得了他們的電子郵件,則很容易會入侵用戶帳戶)。 我不希望用戶在驗證其Facebook帳戶后不必輸入密碼。

我不能同時使用Facebook電子郵件地址和IP地址,因為每次您連接到Internet時,您的IP都會更改。 (不使用密碼即無法登錄)。

以下是Facebook成功登錄后的回復。

email: "my_email address"
first_name: "My Name"
gender: "male"
id: "1508539292713828"
last_name: "My Surname"
link: "https://www.facebook.com/app_scoped_user_id/1508539292713828/"
locale: "en_US"
name: "Full name"
timezone: 2
updated_time: "2014-06-26T08:21:36+0000"
verified: true

您可以在服務器端使用PHP SDK來檢查用戶是否使用以下方式登錄:

// initialize SDK here

// If this returns the user ID, the user is logged in
if ($userId = $facebook->getUser()) {
    try {
        $userData = $facebook->api('/me');
    } catch (Exception $e) {
        echo $e->getMessage();
        exit();
    }

    $email = $userData["email"];

    // Do whatever
}

編輯:流程為:使用JS SDK登錄用戶,然后在FB.login回調中發送AJAX請求,您在此處檢查用戶是否使用PHP SDK登錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM