正如标题所说,我的网站使用 Facebook PHP SDK 时遇到问题。有时我的页面会说我没有登录,但实际上我已经登录了。
我最近写了一个简短的测试用例,它打印出 JavaScript FB.getLoginStatus 响应和一个简短的 PHP 部分,它会写出我是否登录。
这是我的整个tests.php
<?php
// The Facebook PHP SDK v3.2.2
require_once("../facebook/facebook.php");
$config = array();
$config["appId"] = "284773564887682";
$config["secret"] = "it's a secret";
$facebook = new Facebook($config);
// Get User ID
$user = $facebook->getUser();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta property="fb:app_id" content="<?php echo $facebook->getAppID(); ?>" />
<link rel="shortcut icon" href="/img/favicon.ico" type="image/x-icon">
<link href="/bootstrap/css/bootstrap.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js" type="text/javascript"></script>
<title>Rank: The Game - Test Cases</title>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook->getAppID(); ?>', // App ID
channelUrl : '//rankthegame.net/channel.php', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.getLoginStatus(function(response) {
document.getElementById('js').innerHTML = 'JavaScript response.status is: ' + response.status;
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
// Additional initialization code here
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
<div id="js">
</div>
<div id="php">
<?php
if ($user) {
echo "PHP says we are logged in.";
} else {
echo "PHP says we are NOT logged in.";
}
?>
</div>
</body>
</html>
有时,我也会得到这个:
所以我的问题很简单。这是怎么回事?我该如何解决?
我正在使用最新的 PHP SDK (v3.2.2)。
我目前的解决方法是,当我检测到这一点时,我会强制重新加载页面。这通常可以解决问题。