1

我浏览了该网站上的许多帖子,但仍然无法让 Facebook 登录和重定向工作。当用户通过按网页上的按钮通过 Facebook 登录时,我想自动将他们重定向到另一个网页并通过 url 传递他们的电子邮件和密码。不幸的是,我只能让它在他们登录时工作,我必须手动重新加载页面以通过刷新我的网页来让他们进入重定向页面。我想知道是否有人可以发布有关如何执行此操作的代码?我试过很多东西。

<?php

 //uses the PHP SDK.  Download from https://github.com/facebook/php-sdk
 require 'facebook.php';

define('YOUR_APP_ID', 'xxxx');

$facebook = new Facebook(array(
    'appId'  => 'xxxx',
   'secret' => 'xxxx',
  ));   

?>

<div id="fb-root"></div>
<div id="user-info"></div>
     <div class="fb-login-button" style="position:absolute;top:800px;">Login with Facebook</div>




<script>

    window.fbAsyncInit = function() {
      FB.init({ appId: 'xxxx', 
            status: true, 
            cookie: true,
            xfbml: true,
            oauth: true});

  function updateButton(response) {
    var button = document.getElementById('fb-auth');

    if (response.authResponse) {
      //user is already logged in and connected
      var userInfo = document.getElementById('user-info');
      FB.api('/me', function(response) {
        userInfo.innerHTML = '<img src="https://graph.facebook.com/' 
      + response.id + '/picture">' + response.email;
        button.innerHTML = 'Logout';

             });
      button.onclick = function() {
        FB.logout(function(response) {
          var userInfo = document.getElementById('user-info');
          userInfo.innerHTML="";
    });
      };






} 



    else {
      //user is not connected to your app or logged out
      button.innerHTML = 'Login';
      button.onclick = function() {
        FB.login(function(response) {
      if (response.authResponse) {
            FB.api('/me', function(response) {
          var userInfo = document.getElementById('user-info');
          userInfo.innerHTML = 
                '<img src="https://graph.facebook.com/' 
            + response.id + '/picture" style="margin-right:5px"/>' 
            + response.name;


        });    
          } else {
            //user cancelled login or did not grant authorization
          }
        }, {scope:'email'});    
      }
    }
  }


  // run once with current status and whenever the status changes
  FB.getLoginStatus(updateButton);
  FB.Event.subscribe('auth.statusChange', updateButton);    
};

(function() {
  var e = document.createElement('script'); e.async = true;
  e.src = document.location.protocol 
    + '//connect.facebook.net/en_US/all.js';
  document.getElementById('fb-root').appendChild(e);
}());





</script>

<?php

   $uid = $facebook->getUser();
    if($uid != 0) {
        header("Location: signup.php");
      }   

    ?>
4

1 回答 1

-1

主要header('Location')用于 facebook 应用程序不起作用。

我假设其余代码正在运行,因此唯一需要的更改是

$uid = $facebook->getUser();
if($uid != 0) {
    echo("<script> top.location.href='your-login-url'</script>");
    return;
}

希望这可以帮助

于 2012-06-10T14:37:43.740 回答