简体   繁体   English

Facebook身份验证到我的PHP页面

[英]Facebook Authentication to my PHP Page

I am trying to allow users to register on my page using Facebook. 我试图允许用户使用Facebook在我的页面上注册。 This is how I connect / Login to Facebook (Just the JS code). 这就是我连接/登录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'));

My Problem is that I want to to login after it is registered. 我的问题是我要在注册后登录。 I will use a $.post request to register, but now what will be the best way to Authenticate the actual login as you cannot retrieve the users FB password? 我将使用$ .post请求进行注册,但是由于您无法检索用户FB密码,因此现在是验证实际登录名的最佳方法是什么?

I want the page to perform an actual login authentication (From my site) when the user logs into Facebook. 当用户登录Facebook时,我希望页面执行实际的登录身份验证(来自我的网站)。 Example: 例:

There will be 2 methods in which to login to my site. 有两种方法可以登录到我的网站。

  1. Use my login interface (Which will log you in when the username and password is correct). 使用我的登录界面(用户名和密码正确时,这将登录您)。
  2. Use Facebook (Which should then post the data to the same PHP page as the Login through my site - So it would be as if the user logged in through my site). 使用Facebook(然后将数据发布到与通过我的网站登录相同的PHP页面上-就像用户通过我的网站登录一样)。

My first attempt was to use the Persons Email to login to the site (But then a users account can easily be hacked if someone obtains their email). 我的第一个尝试是使用“个人电子邮件”登录到该网站(但是,如果有人获得了他们的电子邮件,则很容易会入侵用户帐户)。 I don't want the user to have to input a password after he has already authenticated his Facebook account. 我不希望用户在验证其Facebook帐户后不必输入密码。

I cant use a combination of Facebook Email and IP address because your IP changes every time you connect to the internet. 我不能同时使用Facebook电子邮件地址和IP地址,因为每次您连接到Internet时,您的IP都会更改。 (Defeats the point of logging in without a password). (不使用密码即无法登录)。

Below is the response from Facebook upon a successful login. 以下是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

You can use the PHP SDK on the server side to check if the user is logged in using: 您可以在服务器端使用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
}

Edit: The flow would be: log the user in using the JS SDK, then send an AJAX request in the FB.login callback, where you check if the user is logged in using the PHP SDK. 编辑:流程为:使用JS SDK登录用户,然后在FB.login回调中发送AJAX请求,您在此处检查用户是否使用PHP SDK登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM