0

index.php:

blablabl
<?php include("fb-api/****-api.php"); ?>
blablabal
    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

**-api.php:

<?php

require 'src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '******',
  'secret' => '***********',
));
$user = $facebook->getUser();
    if ($user) {
      $logoutUrl = $facebook->getLogoutUrl();
    } else {
      $loginUrl = $facebook->getLoginUrl();
    }

?>

link:http://music.x-game.info/ Ok, I login without any problems, but when I press the logout link it doesnt logout me, any ideas?

3 Answers 3

3

Instead of using facebook->getlogouturl redirect the user to another file: logout.php and include this:

  <?php
  $facebook->destroySession();
  session_destroy();
  ?>
1
  • This is NOT ALLOWED by Facebook because it does not sign the user out from Facebook. According to Policies I.6, "Your website must offer an explicit "Log Out" option that also logs the user out of Facebook." taken from developers.facebook.com/policy Apr 12, 2013 at 0:08
1

After logout, user comes your main page and logins automatically because of $user variable will be set.

You can use a database to control logins/logouts.

2
  • Aham, can I make my index only to check if user is logged in, and if isn't NOT to do it?
    – justMemo
    Oct 3, 2012 at 22:15
  • Actually, there is no login/logout function of Facebook. If someone enters the page that runs $user = $facebook->getUser(); code, the $user variable will be always set. You have to use a database that it could be txt file. Check out database if $user['facebookid'] logged out, then redirect to login page.
    – tersakyan
    Oct 3, 2012 at 22:53
0

Set the redirect_uri when creating the logout URL to something like http://yourdomain.tld/logout.php.

In that script, first initialize the Facebook SDK again, call it’s destroySession method, and afterwards call PHP’s own session_destroy.

And then redirect to your start page or whatever.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.