Automatic Login Facebook PHP SDK

瘦欲@ 提交于 2019-12-12 01:45:40

问题


I want something like this:

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?

Thanks


回答1:


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.

Below that add the following 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);
        }());

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
}


来源:https://stackoverflow.com/questions/20810813/automatic-login-facebook-php-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!