views:

132

answers:

2

Hey, I'm writing a canvas app for facebook. I use the command:

$user_id = $facebook->require_login();

To get the userid, but at the first few pages of my app I don't want to require login, but if the user's logged in, I'd like to know what the userid is. Basically I need a get_login() command that perhaps returns 0 or null if the user is not logged in. Is there anything like that?

+2  A: 

Maybe Users.getLoggedInUser() method will help you.

Boris Guéry
Auth.getSession() as well.http://wiki.developers.facebook.com/index.php/Auth.getSession
Jorge
+2  A: 

If you are writing an FBML app, then on every page of your app Facebook will pass a bunch of data in the query string of the page request. These parameters are all prefixed with fb_sig, and make up the validation information for the user. You don't need to query the API at all.

When you instantiate the $facebook object from the standard PHP client library, using something like $facebook = new Facebook('api key','secret key');, the Facebook code automatically validates all these parameters for you and stores the result as part of the Facebook object.

As a result, you can simply do the following to determine if a user is logged in:

$facebook = new Facebook('api key','secret key');
$fbId = $facebook->get_loggedin_user();

If all the proper parameters were received, then you will get the user's Facebook id. I believe you get null otherwise.

Note that if you are making an iframe app, there are other things to consider, as Facebook Connect will set cookies that may not necessarily expire when a user logs out of Facebook. The signature is always valid though, so if you get an id from get_loggedin_user(), you can be sure that it's the right id.

zombat
Ah ok. I thought this was the case, but it wasn't working. After further investigation it appears this wasn't working properly because of the way I sanitized my $_POST array. Thanks!
Eric