【问题标题】:Facebook Authentication to my PHP PageFacebook 身份验证到我的 PHP 页面
【发布时间】:2014-08-05 13:59:42
【问题描述】:

我正在尝试允许用户使用 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 页面 - 这样就好像用户通过我的网站登录一样)。

我的第一次尝试是使用 Persons 电子邮件登录该网站(但是如果有人获得了他们的电子邮件,那么用户帐户很容易被黑客入侵)。我不希望用户在验证他的 Facebook 帐户后必须输入密码。

我不能同时使用 Facebook 电子邮件和 IP 地址,因为每次连接到互联网时您的 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 facebook authentication


    【解决方案1】:

    您可以在服务器端使用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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 2011-07-09
      • 2017-01-18
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多