Facebook login button works only on the second click

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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;         }     } } 

回答1:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!