Facebook API:使用JavaScript SDK登录,然后使用PHP检查登录状态 [英] Facebook API: Login using JavaScript SDK then checking login state with PHP

查看:188
本文介绍了Facebook API:使用JavaScript SDK登录,然后使用PHP检查登录状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击登录按钮时,我使用的是Facebook的JavaScript SDK来弹出一个登录弹出窗口。

I'm using Facebook's JavaScript SDK to bring up a Login Popup when the user clicks the Login button.

代码是按照Facebook在提供的方式提供的:

The code is, as Facebook provides in the doucmentation:

$(".loginButton").click(function(){
 FB.login(function(response) {
    FB.api('/me', function(response) {
       console.log(response.id);
       //User ID shows up so I can see that the user has accepted the app.
     });
 });

我也可以使用 FB.getLoginStatus()来检查用户是否已经登录并接受了该应用程序。

I can also use FB.getLoginStatus() to check that the user has indeed logged in and accepted the application.

但是,现在我想使用PHP执行一个函数,据我所知,PHP在成功登录后被分配了UserID, $ user

However, now I would like to execute a function with PHP. As far as I understand, PHP has $user which is assigned the UserID after a successful login.

问题是在JS登录之后$ code $ $ 仍然是 0 我不知道为什么不会将正确的用户ID分配给 $ user

The problem is after the JS Login $user is still 0. I'm stuck and I can't figure out why it won't assign the correct user ID to $user

这是我的JS:

    <script type="text/javascript">  

        window.fbAsyncInit = function() {
            FB.init({
                appId  : '<?php echo $AppId; ?>',
                status : true, // check login status
                cookie : true, // enable cookies to allow the server to access the session
                xfbml  : true,
                channelUrl  : '<?php echo $ServerPath; ?>channel.php' // custom channel
            });

    }; 


    //Get Login Status
    function amILoggedIn(){
        var toreturn = "";
        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {
                window.userId =  response.authResponse.userID;
            toreturn = "GetLoginStatus: logged in" + " " + "<?php echo $user; ?>";
            } else {
            toreturn = "GetLoginStatus: Logged Out";
            }
            console.log(toreturn);
        }); 


    };


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

            var obj = {
                    method: 'feed',
                    link: 'http://www.google.com'
                };


    $(".loginPopupButton").click(function(){
         FB.login(function(response) {
            FB.api('/me', function(response) { //user-specific stuff
               console.log(response.id);
             });
         });

    });

    </script>

这里是PHP:

    <?php
    include_once('fbapi/facebook.php');
    include_once('fbapi/Authenticated_User.php');
    include_once('config.php');


    // Create our Application instance.
    $facebook = new Facebook(array(
                    'appId'  => $AppId,
                    'secret' => $AppSecret
                ));
    //Facebook Authentication part
    $user = $facebook->getUser();


    $perms = "manage_pages";
    if(isset($_GET['backlink']))
        $redirectUrl   = urldecode($_GET['backlink']);
    else
        $redirectUrl   = $fullserverpath;

    $cancelUrl   = $ServerPath."index.php?page=loginWFb";

    //AA - where to go to get FB popup.    
    $loginUrl = $facebook->getLoginUrl(
            array(
                'scope' => $perms,
                'redirect_uri' => $redirectUrl,
                'cancel_uri' => $cancelUrl
            )
    );



    //AA- Defining that a powerUSER is someone who's logged in
    if(isset($user)){
        $_SESSION['loggedIn'] = $user;

    }

    ?>


推荐答案

使用JS SDK登录将登录数据存储在使用PHP SDK登录时的Facebook Cookie将数据存储在PHP会话中。因此,如果您使用JS SDK登录,则PHP SDK将不会知道。

Login with the JS SDK stores the login data in the Facebook cookies while login with the PHP SDK stores the data in the PHP session. So if you log with the JS SDK, the PHP SDK will not be aware of that.

要使用JS SDK数据登录PHP SDK,您需要调用您的PHP页面与查询参数中签名的请求:

To login with the PHP SDK using the JS SDK data, you need to call your PHP page with the signed request in the query parameter:

FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        // Full redirect or ajax request according to your needs
        window.location.href = 'login.php?signed_request='+response.authResponse.signedRequest;
    }
});

然后 $ facebook-> getUser()将给你的PHP代码中的用户ID。

Then $facebook->getUser() will give you the user id in your PHP code.

这篇关于Facebook API:使用JavaScript SDK登录,然后使用PHP检查登录状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆