cdub
cdub

Reputation: 25701

Facebook Logout with PHP and JS SDK

I have the following code (with my facebook.php correctly added in, appId and appSecret are also correct):

 <?php

 define('APP_ID', 'XXX');

 $facebook = new Facebook(array(
     'appId'  => APP_ID,
     'secret' => 'XXX',
));

$userId = $facebook->getUser();

?>

 <html>
  <body>
 <div id="fb-root"></div>

<?php if ($userId) { 
  $userInfo = $facebook->api('/' + $userId); ?>
  Welcome <?= $userInfo['email'] ?> <fb:logout-button></fb:logout-button>

<?php } else { ?>
<div id="fb-root"></div>
<fb:login-button></fb:login-button>
<?php } ?>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '<?= APP_ID ?>',
      status     : true, 
      cookie     : true,
      xfbml      : true,
      oauth      : true,
    });

FB.logout(function()
{
    top.location.href = 'http://www.google.com'
});

  };

  (function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));
</script>

My code always outputs Welcom [email protected]. How do I get it to logout correctly so that I can test it to login again?

Upvotes: 0

Views: 3375

Answers (2)

McFn
McFn

Reputation: 36

$fb_session = $facebook->setSession(null);

@Somnath Muluk , it seems this code will clear the session, so not just the fb app will logout, the facebook website loginstatus also will be clear.

But i think the app-logout-option will just let the app logout of fb. when I continue to visit my fb feeds it should work without let me do the login option again

Upvotes: 0

Somnath Muluk
Somnath Muluk

Reputation: 57656

Delete cookies, session, etc. Then with the facebook API do something like this:

        $logoutUrl = $facebook->getLogoutUrl(array('next' => $_SERVER['HTTP_REFERER'], 'session_key' => $fb_session['session_key']));

        $fb_session = $facebook->setSession(null);

        header('Location:' . $logoutUrl);

If you want the user to go back to a specific page just change the 'next' value to whatever page you want them to go back to.

Upvotes: 1

Related Questions