views:

25

answers:

1

I have a facebook iframe facebook app. At the top of each page I run the authentication script from the php example included. For some reason however if I login to one facebook account, access my application, log out of facebook and into a new account when I visit the application I am still authenticated as the first user.

How come the session still exists? Why does the Facebook library not realise it is invalid?

please help.

Here is my authentication script that I include at the top of each page. It is almost identical to the example. The only change is that I have added a line to pass a GET parameter through the login url.

 $uid = null; //facebook user id

  require_once "facebook.php";

    // Create our Application instance.
    $facebook = new Facebook(array(
      'appId'  => $conf['fb']['appid'],
      'secret' => $conf['fb']['secret'],
      'cookie' => true,
    ));



    if (is_numeric($_GET['user_id'])) {$user['id'] = $_GET['user_id']; $loginUrlParam = '?user_id='.$_GET['user_id'];}

    //Facebook Authentication part
    $session = $facebook->getSession();
    $loginUrl = $facebook->getLoginUrl(
            array(
            'canvas'    => 1,
            'fbconnect' => 0,
            'req_perms' => 'publish_stream',
            'next' => $conf['dir']['app_url'].$loginUrlParam
            )
    );

    $fbme = null;

    if ($session) {
        try {
            $uid      =   $facebook->getUser();
            $fbme     =   $facebook->api('/me');

        } catch (FacebookApiException $e) {
            echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
            exit;
        }
    }


    print_r($fbme);

Update:

Just found something very weird. I am able to be logged into facebook in two browsers. I know in the past if I logged into one from one from one browser it would log me out of the other. Is there a problem with facebook atm?

+1  A: 

Your problem is actually a combination of issues

First of all, you have cookie support enabled on your instance of the Facebook class. This triggers Facebook::getSession() to look for a session in the cookie if a session is not provided in $_REQUEST.

And, by the look of this code, since you're obtaining the session prior to knowing who the user is, the old session that's still in the cookie is picked up.

Remember, logging out of Facebook is going to do nothing to delete/remove session cookies on your domain.

You have several ways around this. If you're going to keep cookie support enabled, then you'll need to make sure you properly reset that cookie when an un-identified user visits the application. This is something you would have to do on a fresh load of the canvas page - not something that's included to every page of your app.

$facebook = new Facebook(array(
  'appId'  => $conf['fb']['appid'],
  'secret' => $conf['fb']['secret'],
  'cookie' => true,
));

// Calling this w/no parameters will clear the session
$facebook->setSession();

You can also just turn cookie support off but then you'll need to manually maintain the session ID and facebook session data as well.

Peter Bailey
Thanks for the reply. How would I know if an 'un-identified user' visits the application? The user id that $facebook gives me is the one of the 'first user'...
Pablo
I just re read your answer you said 'First of all, you have cookie support enabled on your instance of the Facebook class. This triggers Facebook::getSession() to look for a session in the cookie if a session is not provided in $_REQUEST.' A session is provided in $_REQUEST though as I can see it is in the url of the iframe. So why did the facebook lib not detect it?
Pablo
Depends. Have you migrated to the `signed_request` parameter?
Peter Bailey
I am sorry for sounding like a complete n00b but what exactly do you mean by 'migrated to the signed_request'?
Pablo
In the application configuration, under `Advanced >> Migrations`, the current 4th option is labeled **OAuth 2.0 for Canvas** which replaces the `session` parameter with a `signed_request` one instead.
Peter Bailey
Ok, I migrated to 'signed_request'. Strangely though regardless of the $_GET parameters in the url of the iframe is still showing the old users data
Pablo
scrap that... that last thing did fix it... just took a little while to update I guess
Pablo