1
votes

I have a Facebook "login button" on my site for the purpose of getting user information. It was working until Facebook switched things over recently. It used to refresh the page when the user clicked "login" and then I could access all of their information using the cookie.

Login button code(HTML):

<fb:login-button perms="email" oauth="true" style="float:left;"></fb:login-button>

Getting the user's information using the cookie (PHP):

$user = json_decode(file_get_contents('https://graph.facebook.com/me?access_token=' . $cookie['access_token']));

This code was at the bottom of my page:

 <div id="fb-root"></div>
 <script src="http://connect.facebook.net/en_US/all.js"></script>
 <script>
   FB.init({appId: '<?= FACEBOOK_APP_ID ?>', status: true,
            cookie: true, xfbml: true});
   FB.Event.subscribe('auth.login', function(response) {
     window.location.reload();
   });
 </script>

And I had this PHP function for the purpose of getting the cookie:

 function get_facebook_cookie($app_id, $application_secret) {
   $args = array();
   parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
   ksort($args);
   $payload = '';
   foreach ($args as $key => $value) {
     if ($key != 'sig') {
       $payload .= $key . '=' . $value;
     }
   }
   if (md5($payload . $application_secret) != $args['sig']) {
     return null;
   }
   return $args;
 }

 global $cookie;
 $cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);

Now it seems that auth.login does not fire. When a user clicks on the "login" button, the popup shows. But after the user allows the app, the popup closes and nothing else happens. Before, it would reload the current page. This is (or was) triggered by the auth.login which is what leads me to believe that part is broken. However, I tried replacing auth.login with auth.statusChange which simply caused the page to be refreshed over and over again as it was getting called everytime the page loaded. At the end of the day, it seems my cookie is not being created any longer. I've done a lot of digging to see if anyone else had this issue but couldn't find anything helpful.

I believe the main problem is that the cookie is not being created/populated.

1
Is this happening on localhost? I've been getting the same problem on a project that has the same problem on localhost, but works fine on a different server. FB.login() works fine with the exception that it does not place an fbsr_ cookie.Attila Szeremi

1 Answers

0
votes

in your FB.init, try add oauth: true

FB.init({appId: '<?= FACEBOOK_APP_ID ?>', status: true, cookie: true, xfbml: true, oauth:true});