我正在使用 webtechnick Facebook 插件,并已成功将其与我的 PHP 站点集成。登录和注销工作正常,但是当我在一个选项卡中登录 Facebook 并在另一个选项卡中登录我的网站时遇到问题。
在这种情况下,我的网站会自动检索 Facebook 数据,而无需单击我登录页面中的 Facebook 登录按钮。仅当我单击登录页面中的 Facebook 登录按钮时,该网站才应检索 Facebook 数据。我该如何解决这个问题?
这是我在 FacebookHelper.php 中的代码:
public function init($options = null, $reload = true) {
$options = array_merge(array(
'perms' => 'email'
), (array)$options);
if ($appId = FacebookInfo::getConfig('appId')) {
$init = '<div id="fb-root"></div>';
$init .= $this->Html->scriptBlock("
window.fbAsyncInit = function() {
FB.init({
appId : '$appId', // App ID
channelURL : '../../Vendor/channel.php', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
// Checks whether the user is logged in
FB.getLoginStatus(function(response) {
if (response.authResponse) {
// logged in and connected user, someone you know
// alert('You are connected');
} else {
// no user session available, someone you dont know
// alert('You are disconnected');
}
});
FB.Event.subscribe('auth.authResponseChange', function(response) {
if (response.authResponse) {
// the user has just logged in
// alert('You just logged in facebook from somewhere');
} else {
// the user has just logged out
// alert('You just logged out from faceboook');
}
});
// Other javascript code goes here!
};
// logs the user in the application and facebook
function login(redirection){
FB.login(function (response) {
if(response.authResponse) {
// user is logged in
// console.log('Welcome!');
if(redirection != null && redirection != ''){
top.location.href = redirection;
}
} else {
// user could not log in
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: '" . $options['perms'] . "'});
}
// logs the user out of the application and facebook
function logout(redirection){
FB.logout(function(response) {
// user is logged out
// redirection if any
if(redirection != null && redirection != ''){
top.location.href = redirection;
}
});
}
// Load the SDK Asynchronously
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol
+ '//connect.facebook.net/".$this->locale."/all.js';
document.getElementById('fb-root').appendChild(e);
}());");
return $init;
} else {
return "<span class='error'>No Facebook configuration detected. Please add the facebook configuration file to your config folder.</span>";
}
}
登录页面(login.ctp):
<?php
echo $this->Facebook->html();
echo $this->Facebook->login(array('id'=>'facebookbut','img' => 'facebooklogin.png','redirect' =>'/'));
echo $this->Facebook->init();
?>