Facebook登录按钮仅在第二次单击时有效 [英] Facebook login button works only on the second click

查看:66
本文介绍了Facebook登录按钮仅在第二次单击时有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php facebook类,并使用$f->getLoginUrl()在我的页面上放置了一个登录按钮,问题是该按钮在我第一次单击时不起作用.它会执行预期的操作并返回我的站点,但$f->getUser()为空. 奇怪的是,当我第二次单击该按钮时,它可以正常工作.

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. It does what it's supposed to and returns to my site but the $f->getUser() is empty. 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;
        }
    }
}

推荐答案

您是否已在JavaScript部分中添加了标准FB处理程序?他们应该重新加载页面并设置一些cookie,以便使Facebook API正常工作...

Have you added the standard FB handlers in your javascript portion? They are supposed to reload the page and set some cookies for the facebook API to work...

<!-- 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 -->

这篇关于Facebook登录按钮仅在第二次单击时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆