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!