Jean-François Savard
Jean-François Savard

Reputation: 21004

Check if already logged to facebook with PHP SDK 4.0

I want to check if already logged in to facebook when navigating to index.php so I would automatically redirect to homepage.

However, my code will only check after clicking on the loggin link.

Facebook\FacebookSession::setDefaultApplication(Globals::FB_APP_ID, Globals::FB_APP_SECRET);

$helper = new Facebook\FacebookRedirectLoginHelper("myurl");

try {
        $session = $helper->getSessionFromRedirect();

} catch (FacebookRequestException $ex) {
    // When Facebook returns an error
} catch (Exception $ex) {
    // When validation fails or other local issues
}

// see if we have a session
if (isset($session)) {
    // graph api request for user data
    $request = new FacebookRequest($session, 'GET', '/me');
    $response = $request->execute();
    // get response
    $graphObject = $response->getGraphObject();

    header("location:home.php");
} else {
    // show login url
    echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}

I checked some other thread about Facebook PHP SDK but there is not much about sdk 4.0. And nothing that worked for me..

I tought I could add a token in the session, however it wouldn't work if I would already be logged in to facebook before my first visit... It would also cause problem if I would logout from facebook without logout from my website.

Upvotes: 3

Views: 3176

Answers (1)

Jean-Fran&#231;ois Savard
Jean-Fran&#231;ois Savard

Reputation: 21004

Since I found this question to be frequently asked without answer, I decided to share what I did with the community.

The $session variable in my code contains an access token which can be used for re-instanciating the session anywhere else simply doing a new FacebookSession('token').

Considering that, when logging in for the first time, I retrieve the token from the $sessionand store it in $_SESSION. Then, I created a simple util function which first check if the $_SESSIONvariable is set, then try to instanciate a new FacebookSession using the stored token. If the stoken is invalid, the session won't instanciate then I can detect it in my util function and return wether the user is actually logged in or not.

Note that an access token is invalidated when facebook user logout.

Login url + token storage :

FacebookSession::setDefaultApplication(Globals::FB_APP_ID, Globals::FB_APP_SECRET);


$helper = new Facebook\FacebookRedirectLoginHelper("http://localhost/PoubellesDeMerde/PoubellesDeMerde/index.php");

try {
        $session = $helper->getSessionFromRedirect();

} catch (FacebookRequestException $ex) {
    // When Facebook returns an error
} catch (Exception $ex) {
    // When validation fails or other local issues 
}

// see if we have a session
if (isset($session)) {
    // graph api request for user data
    $request = new FacebookRequest($session, 'GET', '/me');
    $response = $request->execute();

    $_SESSION['token'] = $session->getToken();
} else {
    // show login url
    echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}

Util function :

public static function isLoggedIn()
{
    $id=0;       
    if(isset($_SESSION['token']))
    {
        $session = new FacebookSession($_SESSION['token']);

        $request = new FacebookRequest($session, 'GET', '/me');
        $response = $request->execute();

        $graphObject = $response->getGraphObject();

        $id = $graphObject->getProperty("id");                        
    } 

    return $id!=0;
}

Upvotes: 1

Related Questions