不知何故Facebook登录不起作用,因为faveicon有一个空的href

时间:2015-03-17 07:27:51

标签: php facebook facebook-php-sdk

更新

所以我终于找到了问题所在。具有空href属性的 favicon

<link rel="icon" href="">

它是Facebook API的州值,但它没有提出任何额外的GET请求。此外,如果我在此行之前有任何输出,它将按预期工作。

我想知道这怎么会导致这样的问题,即使没有提出任何额外的HTTP请求?在这条线的上方和下方还有其他输出,添加或删除它们中的任何一个都没有任何区别。这一小段代码有什么特别之处?它实际上是否可以发送未在浏览器控制台中显示的HTTP请求?

原始问题

我在类中抽象了Facebook SDK v4,如下所示。我面临的问题是,在某些情况下,$cfg['facebook']['login_redirect_url']页面上的FB::CheckLogin()无效,除非我向页面输出内容。

我想知道这里有什么东西我不见了或者我忽略了。

$cfg['facebook']['login_redirect_url']页面:

if ( FB::CheckLogin() ) {

    $user_facebook_data = FB::UserData();

    /* Setting up user session ... */

} else {
    throw new Exception( 'Facebook Login Failed' );
}

header( 'Location: ' . $cfg['url'] . '/' );

这包含在每个页面中:

FB::AppID( $cfg['facebook']['app_id'] );
FB::AppSecret( $cfg['facebook']['app_secret'] );
FB::LoginRedirect( $cfg['facebook']['login_redirect_url'] );
FB::Scope( 'email' );

FB::Start();
FB::Connect();

我的Facebook抽象:

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;

abstract class FB {

    private static $app_id = null;
    private static $app_secret = null;
    private static $login_redirect_url = null;
    private static $scope = null;
    private static $helper = null;
    private static $login_url = null;
    private static $session = null;
    private static $user_data = null;
    private static $login = false;
    private static $redirect_uri = null;

    public static function Start () {

        require_once __DIR__ . '/../libraries/facebook-sdk/autoload.php';

        FacebookSession::setDefaultApplication( self::$app_id, self::$app_secret );

        self::$helper = new FacebookRedirectLoginHelper( self::$login_redirect_url );
    }

    public static function Connect () {

        if ( !empty( $_SESSION ) && !empty( $_SESSION['fb_token'] ) ) {

            self::$session = new FacebookSession( $_SESSION['fb_token'] );

            try {

                if ( self::$session->validate() ) {
                    self::$login = true;
                } else {
                    self::$session = null;
                    self::$login = false;
                }

            } catch ( Exception $e ) {
                self::$session = null;
            }

        } else {

            try {
                self::$session = self::$helper->getSessionFromRedirect();
            } catch( FacebookRequestException $e ) {
                echo $e->message;
            } catch( Exception $e ) {
                echo $e->message;
            }
        }

        if ( !empty(self::$session) ) {
            self::$login = true;
        }

        if ( self::CheckLogin() ) {

            $_SESSION['fb_token'] = self::$session->getToken();
            self::$session = new FacebookSession( self::$session->getToken() );

            $request = new FacebookRequest( self::$session, 'GET', '/me' );
            $response = $request->execute();
            self::$user_data = $response->getGraphObject()->asArray();
        }
    }

    public static function AppID ( $app_id ) {
        self::$app_id = $app_id;
    }

    public static function AppSecret ( $app_secret ) {
        self::$app_secret = $app_secret;
    }

    public static function LoginRedirect ( $login_redirect_url ) {
        self::$login_redirect_url = $login_redirect_url;
    }

    public static function Scope ( $scope ) {
        self::$scope = $scope;
    }

    public static function UserData () {
        return self::$user_data;
    }

    public static function LoginURL () {

        if ( empty(self::$login_url) ) {

            self::$login_url = self::$helper->getLoginUrl( array(
                    'scope' => self::$scope
                )
            );
        }

        return self::$login_url;
    }

    public static function CheckLogin () {

        if ( !empty(self::$session) && true === self::$login ) {
            return true;
        }

        return false;
    }

    public static function RedirectURI ( $redirect_uri ) {
        self::$redirect_uri = $redirect_uri;
    }

    public static function FixURLHash () {
        ?>
        <script type="text/javascript">
            if (window.location.hash && window.location.hash == '#_=_') {
                if (window.history && history.pushState) {
                    window.history.pushState("", document.title, window.location.pathname);
                } else {
                    var scroll = {
                        top: document.body.scrollTop,
                        left: document.body.scrollLeft
                    };
                    window.location.hash = '';
                    document.body.scrollTop = scroll.top;
                    document.body.scrollLeft = scroll.left;
                }
            }
        </script>
        <?php
    }
}

2 个答案:

答案 0 :(得分:0)

在为您的网页创建Facebook应用时检查您的域名网址。这可能是&#34; http://&#34;或&#34; https://&#34;。删除&#34; http://www。&#34;或&#34; https://www。&#34;来自你的域名网址在Facebook应用程序。只保留&#34; urdomain.com&#34;。希望这有效

答案 1 :(得分:0)

为什么不尝试使用JavaScript重定向,而不是执行标头重定向。变化:

header( 'Location: ' . $cfg['url'] . '/' );

为:

echo '<script>window.location = "'. $cfg['url'] .'";</script>';

这可以帮助您解决向页面输出内容并保持重定向的问题。