I've been searching for hours for the solution to this problem but can't find one that works for me. When i click "Logout" on my site the user information is still visible and the logout button is still displayed. Here is the code:
require 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxx',
'secret' => 'xxxx',
));
// Get User ID
$user = $facebook->getUser();
var_dump($user);
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($_GET['logout'] == "yes") {
setcookie('fbs_'.$facebook->getAppId(), '', time()-100, '/', 'http://gno.....ment/index.php');
session_destroy();
header("Location: ".$_SERVER['PHP_SELF']."");
}
if ($user_profile) {
$logoutUrl = $facebook->getLogoutUrl;
} else {
$loginUrl = $facebook->getLoginUrl(array('scope' => 'email,publish_stream,user_status',
'canvas' => 1,
'fbconnect' => 0,
'redirect_uri' => 'http://gno.....ment/index.php'));
}
..... .....
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout of FB</a>
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
</div>
<?php endif ?>
It seems that if ($_GET['logout'] == "yes")
might be the answer for me but i can't get it working. I don't know where logout
is gotten from or where it is defined?
This seems to be a common issue but i can't figure it out. I'd really appreciate some help.
Here is how I logout using the latest PHP-SDK:
login.php
logout.php
Because I still have PHP 5.3 at my CentOS 6.7 server in year 2016 and don't want to take the hassle of upgrading the PHP version - I still use the old facebookarchive/facebook-php-sdk instead of the newer facebook/facebook-php-sdk-v4 library.
And here is how I handle the logout in my app:
Do not forget to -
https://example.com/myapp/
at the same placeDoing it with PHP SDK is really easy, the documentation is just really awfull. You do not need to redirect to Facebook. You just have to clear the session that the Facebook class sets, there is a function for that in the Facebook base class called destroySession(). Here I'm doing it on a get.
The $facebook->getLogoutUrl() logs the user out of Facebook.
Had some kind of similar trouble with that. Even
didn't work properly until I removed
call completely.
getLogOutUrl()
added some parameter that conflicted later with my .htaccess and led to *"mod_fcgid: stderr: CSRF state token does not match one provided"* error.To answer directly to your question
When you create your logout url, add additional parameter 'logout'
Then in your script, you clear session and cookies when
isset($_GET['logout'])
I remember this was a huge pain in one of my apps. It seems that finally what seemed to work was:
I should be about the same without jQuery (just run FB.logout() at page load). AFAIR I just could not get this to work on the server-side in PHP. Hope it helps :).