FB登录无法正常工作(PHP + JS SDK)

时间:2014-01-14 11:30:38

标签: javascript php facebook

我正在尝试获取我的应用程序的第3天fb连接登录/注销正常工作,所以我可以转向设计的新事物。

这让我感到疯狂,我似乎无法找到很多解释或明确说明我做错了什么。

最初我使用FB PHPSDK编写了一个有效的FB连接登录/注销流程。

我只使用FB PHPSDK的问题(在我过去3天完成的整个版本控制支撑之前),我在工作中保存了一些测试代码并造成了半工作代码的丢失。但是,每当我登录并重新访问我本地的Web应用程序而不关闭浏览器时,它将要求我刷新页面(即使用户仍然登录到我的应用程序)以查看隐藏的元素( !$用户。)

我认为这是因为我没有使用cookies / JS进行身份验证。显然两者都“通过fb登录创建无缝体验”,但我读过很多人在使用FB JS SDK + PHP SDK时遇到很多麻烦。

这是我目前的代码,我只是在MAMP / localhost上开发。

我可能不得不重写整件事,但在此之前,我想知道是否有人能够查明我的问题。

我做错了吗? authlink有效,但由于某种原因,登录链接不想工作。

//The code is for sign-in/sign-out in index page.

<div id="fb-root"></div>

<?php if(!$user){?>
<script>
document.getElementById('authLink').href="<?php echo $loginUrl; ?>"
document.getElementById('authLink').innerHTML="Sign-in";
</script>

<? } else if($user){ ?>
<script>
document.getElementById('authLink').href="<?php echo $logoutUrl; ?>"
document.getElementById('authLink').innerHTML="Sign-out";
</script>
<? } ?>

<?php if ($user) { ?>
<!-- if fb app is authorized, show content.-->
// cool design stuff here

<?php } ?>

<script>
  window.fbAsyncInit = function(){
    FB.init({
      appId: '<?php echo $facebook->getAppID() ?>',
      cookie: true,
      xfbml: true,
      oauth: true
    });
    FB.Event.subscribe('auth.login', function(response) {
      window.location.reload();
  }
    });
    FB.Event.subscribe('auth.logout', function(response) {
      window.location.reload();
  }
     });
  (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);
  }());
  }

//FB config file

<?php require_once 'fbphpsdk/src/facebook.php';

$facebook = new Facebook(array(
'appId'  => 'xxxxxxx',
'secret' => 'xxxxxxx',
'allowSignedRequest' => false // optional but should be set to false for non-canvas app
 ));

$params = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'localhost/index.php'
);

// See if there is a user from a cookie
$user = $facebook->getUser();

if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me','GET');
$logoutUrl = $facebook->getLogoutUrl();
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
else { $loginUrl = $facebook->getLoginUrl($params);}

?>

-

//the login element (is in header file)
<div style="color: white;font-weight: 100;text-shadow: 0pt 0pt 5pt #000;float: right;position: relative;top: 13pt;right: 30pt;font-size: 10pt;">
<a id="authLink" style="margin: 0pt 0pt;padding: 2pt 0pt;text-decoration: none;border-bottom: 1px solid #f00;color: white;font-size: 110%;top: 2%;position: fixed;z-index:999;right: 2%;"></a>
</div>

0 个答案:

没有答案
相关问题