简体   繁体   English

Facebook登录按钮仅在第二次单击时有效

[英]Facebook login button works only on the second click

I'm using the php facebook class and put a login button on my page with $f->getLoginUrl() , the problem is this button does not work at first time I click on it. 我正在使用php facebook类,并使用$f->getLoginUrl()在页面上放置一个登录按钮,问题是此按钮在我第一次单击时不起作用。 It does what it's supposed to and returns to my site but the $f->getUser() is empty. 它会执行预期的操作并返回到我的站点,但是$f->getUser()为空。 Strange thing is when I click the button the second time then it works flawlessly. 奇怪的是,当我第二次单击该按钮时,它可以正常工作。

$facebook = new Facebook(array(
    'appId'  => '25317735807xxxx',
    'secret' => '37e5dd05657f2cc2a1bdc9974985xxxx',
    'redirURL'  =>  'http://mysite.com/',
));

$fb_login = '<a href="' . $facebook->getLoginUrl() . '" class="fb-login-button"><img src="img/fb-login-button.png" border="0" /></a>';

$fb_user = $facebook->getUser(); 

$user_level = 4;
if($page_access[$glob['pag']]['session'])
{
    session_start();
}

if ($_SESSION[U_ID])
{
    $user_level = $_SESSION['access_level'];
    require_once("classes/cls_auth.php");
    $auth = new auth();
    $show_connect_alert = $auth->check_connection();
    if($glob['pag'] == 'register' && $_SESSION[U_ID])
    {
        $glob['pag'] = 'profile';
    }
}
else
{
    //check if fb session exists
    if ($fb_user) 
    {
        try 
        {
            $db = new mysql_db();
            $db->query("SELECT email, password FROM member WHERE fb_id = $fb_user");
            if ($db->move_next())
            {
                $glob['email'] = $db->f('email');
                $glob['password'] = $db->f('password');
                require_once("classes/cls_auth.php");
                $auth = new auth();
                $auth->login($glob);
            }
            else 
            {
                $user_profile = $facebook->api('/me');
                $glob['pag'] = "fboptions";
            }
        } 
        catch (FacebookApiException $e) 
        {
            $glob['fb-error'] = $e;
            unset($fb_user);
            $user_level = 4;
        }
    }
    else 
    {
        // check login cookie
        if(isset($_COOKIE['stagescan']))
        {
            list($arr['email'], $arr['password']) = explode(",", $_COOKIE['stagescan']);
            require_once("classes/cls_auth.php");
            $auth = new auth();
            if(!$auth->login($arr))
            {
                setcookie('stagescan', "", time()-3600, "http://mysite.com/");
            }
            else 
            {
                $glob['debug'] = "User logged in from cookie";
            }
        }
        else 
        {
            $user_level = 4;
        }
    }
}

Have you added the standard FB handlers in your javascript portion? 您是否已在JavaScript部分中添加了标准FB处理程序? They are supposed to reload the page and set some cookies for the facebook API to work... 他们应该重新加载页面并设置一些cookie以使facebook API正常工作...

<!-- FACEBOOK JAVASCRIPT HANDLER START -->
<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function() {
        FB.init({
            appId: '%CONTEST_FACEBOOKAPPID%',
            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);
    }());
</script>
<!-- FACEBOOK JAVASCRIPT HANDLER END -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM