webtechnick Facebook插件自动登录问题

时间:2013-06-11 06:47:19

标签: php cakephp-2.0

我使用的是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();
    ?>

0 个答案:

没有答案
相关问题