自动登录 Facebook PHP SDK



>我想要这样的东西:

当用户登录到Facebook时,当他们访问我的网站时,他们不需要再次登录过程。我想像 SoundCloud.com 一样自动捕获登录数据。

有人知道如何做到这一点吗?

谢谢

假设您的登录页面是登录.php然后在页面底部添加一个id id="fb-root"的div元素,然后关闭该div。

下面添加以下 JS

 window.fbAsyncInit = function()
        {
            FB.init
            ({
                appId   : 'your fb app id',
                status  : true, // check login status
                cookie  : true, // enable cookies to allow the server to access the session
                xfbml   : true, // parse XFBML
                oauth   : true
            });
            FB.Event.subscribe('auth.login', function()
            {
                window.location.reload();
            });
        };
      (function()
      {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
        }());

现在在页面顶部使用以下PHP代码

require_once 'src/facebook.php'; //include the facebook php sdk
$facebook = new Facebook(array(
    'appId'  => 'XXXXXXXXXXXXXX',    //app id
    'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX', // app secret
));
$user = $facebook->getUser();
if($user){
  // then do the login script if you have one and then redirect to the page after login
}else{
  // display the fb login button and make login after redirect as login.php
}

最新更新