Question

I had the sample facebook login implementation, and just realized that it stopped working:

 <?php
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'myappid',
  'secret' => 'mybigsecreet',
  'cookie' => true,
));

// We may or may not have this data based on a $_GET or $_COOKIE based session.
//
// If we get a session here, it means we found a correctly signed session using
// the Application Secret only Facebook and the Application know. We dont know
// if it is still valid until we make an API call using the session. A session
// can become invalid if it has already expired (should not be getting the
// session back in this case) or if the user logged out of Facebook.
$session = $facebook->getSession();

$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');

        $r = new registro_usuarios();
        $r->facebook($uid,$me['name'],'https://graph.facebook.com/'.$me['id'].'/picture');

        echo '----------------------------'.$me;
  } catch (FacebookApiException $e) {
    error_log($e);
    echo '----------------------------'.$e;
  }
}else echo 'nosession';
echo $session;

prints nosesion but when i click the login button (facebook) firebug logs: FB.login() called when user is already connected. and the login popup (facebook) won't open.

What am I missing? Is the facebook-api outdated one year later??

Was it helpful?

Solution

$facebook->getSession() is no longer working. That's why it might be exiting from 1st if loop.

$session = $facebook->getSession();

$session is always null.

You can check seesion by $facebook->getUser() if it gives 0 then there is no active session else you will get facebook id of session user.

See this links also might get more inforamation:

OTHER TIPS

You should download the latest SDK from Github at https://github.com/facebook/php-sdk

Then do the following and a user session will be logged.

require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '344617158898614',
  'secret' => '6dc8ac871858b34798bc2488200e503d',
));

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}

This is taken from the example code. Works just fine for me.

Since 1 october OAuth 2.0 authentication is compulsory (see the changes roadmap here: https://developers.facebook.com/roadmap/completed-changes/) and you must change some functions that no longer are supported.

You can check the new php documentation here: https://developers.facebook.com/docs/reference/php/

And the authentication documentation: https://developers.facebook.com/docs/authentication/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top