自动登录Facebook PHP SDK [英] Automatic Login Facebook PHP SDK

查看:159
本文介绍了自动登录Facebook PHP SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要这样的东西:

用户登录到Facebook后,因此当他们访问我的网站时,无需再次进行登录过程.我想像SoundCloud.com一样自动捕获登录数据.

When the user has been logged in to Facebook, so when they go to my website, they do not need to make a log in process again. I want to catch the login data automatically like SoundCloud.com did.

任何人都知道如何进行这项工作吗?

Anyone knows how to make this work?

谢谢

推荐答案

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

Lets say your login page is login.php then at the bottom of the page add an div element with the id id="fb-root" and just close that 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代码

Now on the top of the page use the following PHP code

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
}

这篇关于自动登录Facebook PHP SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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