首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >请求帮助解决网站的Facebook登录问题

问请求帮助解决网站的Facebook登录问题
EN

Stack Overflow用户
提问于 2013-05-09 21:03:28
回答 1查看 96关注 0票数 0

我想实现“登录Facebook”,这是常见的网站,但我有困难掌握整个概念,如果有人可以帮助我,我将不胜感激。

几天来,我一直在阅读这个链接https://developers.facebook.com/docs/facebook-login/下的文档,但我仍然不“理解”。

让我们从..。

在文档中,它说JS SDK最容易使用(我不知道对谁来说),但我认为非JS版本会更好、更快(对我来说也更容易掌握),但我不知道该怎么办。我“卡住了”

我的主要问题是,我不明白如何能够插入一个自动递增ID,以及个人的名字和姓氏到我的数据库中。Facebook代码中没有或根本没有我可以指定数据库、表或列的地方,那么这些数据应该如何进入我的数据库以登录用户呢?

我在我的网站上有一个分类广告,我正试图为它创建一个FB登录,但正如我上面所说的,我被卡住了,需要帮助,因为我只有关于PHP和MySql的中等水平的知识

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-09 22:29:06

与注释中所讨论的一样,下面是使用JS SDK的示例

代码语言:javascript
复制
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <!-- jQuery library, makes things easier. -->
        <title>FB example</title>
    </head>
    <body>
    <div id="fb-root"></div>
    <script>
        window.fbAsyncInit = function() {
            // init the FB JS SDK
            FB.init({
                appId      : YOUR_APP_ID,                        // App ID from the app dashboard
                status     : true,                                 // Check Facebook Login status
                xfbml      : true                                  // Look for social plugins on the page
            });
// Additional initialization code such as adding Event Listeners goes here
    };

    // 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'));


    /*
        This function gets information about user and alrets user's name.
     */
    function getUserInfo() {
    FB.api('/me', function(response) {
        $.ajax({
            url: "ajax.php", //url to send ajax request to
            data: {
                json: response, // Key => value pairs of items to send
            },
            success: function(ajaxResponse) { // upon successful request
                alert(ajaxResponse); //Alert whatever response we got.
            }
        })
    })
}

    /*
        This function gets the login status of user
     */
    function getLoginStatus() {
        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') { //User is connected to Facebook and authorized your app
                getUserInfo();
            } else if (response.status === 'not_authorized') { //User is connected to Facebook, but hasn't authorized your app
                login();
            } else { //User is not connected to Facebook
                login();
            }
        });
    }

    /*
        Function promts user to log in and asks for permissions
     */
    function login() {
        FB.login(function(response) {
            if (response.authResponse) {
                getUserInfo();
            } else {
                alert('User cancelled login or did not fully authorize.');
            }
        }, {scope: 'email'}); // Comma separated permissions
    }

</script>

<a href="javascript:getLoginStatus()"/>Login</a>

</body>
</html>

这是一个非常简单的例子。在页面上,您将看到链接“登录”。一旦按下它,你就会被要求登录(如果你没有登录Facebook,或者你还没有授权应用程序),或者它会弹出你的名字。大部分代码都取自JavaScript SDK documentation

编辑:

要将用户数据添加到数据库中,我建议从getUserInfo()函数调用AJAX。将警告部分替换为对某个php文件的ajax调用,您将创建该文件并将来自response变量(包含有关用户的数据,如response.name和response.email)的数据传递给该文件。在php文件中,将获得的信息插入到数据库中。

另一个编辑:

我再次更新了代码,即添加了jQuery库(对我来说,在其中进行ajax调用更容易,尽管它与普通的JS没有太大区别)。

代码语言:javascript
复制
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

并更新了向ajax.php发出ajax请求的getUserInfo()函数。

在ajax.php中,我只做了一行:

代码语言:javascript
复制
print_r($_REQUEST['json']);

这将简单地打印出通过ajax发送的任何数据,您将在弹出窗口中看到它。下一步是检查您获得了什么信息,并获取您需要的任何信息(它的$_REQUEST['json']是简单的数组),并将其插入到db中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16462497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档