PHP SDK Requires two logins...doesn't recognize first

Posted by Jay Konieczny on Stack Overflow See other posts from Stack Overflow or by Jay Konieczny
Published on 2012-01-24T07:29:53Z Indexed on 2013/10/28 21:54 UTC
Read the original article Hit count: 143

Filed under:
|
|
|

I am using the following code to authenticate whether users are logged in or not. While users can log in, they have to click the login button twice. Additionally, sometimes even after they click the log-in button twice, my "user info" part of the page (earlier in the page than the content) shows them as logged out while the actual page shows them as logged in.

Here is the code. Could someone suggest a better way of handling log-ins?

function isLoggedIn($facebook) {
    if (isset($facebook) and $facebook->getUser() != 0) {
        // UserID exists, but user may still not be logged in. Let's check:
        try {
            $facebook->api('/me', 'GET');
            // If this succeeds, then they are logged in.
            return true;
        } catch(FacebookApiException $e) {
            // Some kind of error, so not logged in.
            if(session_id() === '')
                session_destroy();
            return false;
        }
    } else {
        if(session_id() === '')
            session_destroy();
        return false;
    }
}

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about facebook