Check if connected to app, if not redirect to Login with Facebook PHP SDK / API

I'm struggling to find a way to detect if users are not connected to my application, I want to send the activity 'View' an 'Item' which works perfectly fine, but they have to click a login link first, when just reloads the page and they are logged in.

How can I make this happen automatically? How can you check if the user is connected to the app? I have tried the following code, but users who are not connected just get a blank page.

$facebook = new Facebook($config);
$user_id = $facebook->getUser();

    // Check if logged in
    if($user_id) {
            $params = array(
              'ok_session' => 1, // Logged in and connected
              'no_user' => 2, // Logged out of facebook
              'no_session' => 3, // Logged in but not connected
            );
            $next_url = $facebook->getLoginStatusUrl($params);

            if($next_url == 2 || $next_url == 3){
                header('Location: '.$facebook->getLoginUrl(array('scope' => 'publish_stream')));
            }
        } else {
            // Not logged in
            header('Location: '.$facebook->getLoginUrl(array('scope' => 'publish_stream')));
    } 

There doesn't seem to be a function that checks if the user is connected so I made use of the getLoginUrl function.

$user_id is supplied by $user_id = $facebook->getUser();

Answers


If we are talking canvas/page tab app here, then the info you’re looking for is in the signed_requestparameter your app gets passed on initial load.

If not – “outside” of Facebook the PHP SDK has no way of knowing if there’s a user visiting your page that has used your app before if there aren’t any cookies set for your domain remaining that say so.

You can use FB.getLoginStatus out of the JS SDK – that will make a cross-domain request to Facebook to see if the user is logged in, and can set the appropriate cookies under your domain that’ll let the PHP SDK take notice on the next request to your server as well.


I don't know you found it or not but here is the simple tutorial come example by Facebook itself. Anyone getting started with Facebook Development can find this at first place on Google. I hope you found it too... just for people searching something like this and not found answer to it. here is the link:

https://developers.facebook.com/docs/howtos/login/getting-started/

this article also might be helpful:

http://thinkdiff.net/facebook/new-javascript-sdk-oauth-2-0-based-fbconnect-tutorial/

Thanks :)


Need Your Help

Animated textView change the text and animate again (animated countdown)

java android android-animation

I am trying to make an animated countdown on android plataform, that show de number 3 in textView growing up from 0 to 1.5 on scale property, so the number 2, 1 and "GO!".

Alternative methods of locating function offsets for use as function pointer?

c++ function-pointers reverse-engineering

When writing code that is to be injected into a running process, and subsequently call functions from within that application, sometimes you need to create a function pointer if you're wanting to c...