I am using the official Facebook PHP SDK for my website. My application lets the users log in and out. Problem is, you can’t really logout although i use the example of facebook. When i click “Logout” on my site the user information is still visible and the logout button is still displayed. Here is the code:
<?php require './src/facebook.php'; // Create our Application instance (replace this with your appId and secret). $facebook = new Facebook(array( 'appId' => '111111111111', 'secret' => '1111111111111111111111', )); // Get User ID $user = $facebook->getUser(); 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 ($user) { $logoutUrl = $facebook->getLogoutUrl(); } else { $loginUrl = $facebook->getLoginUrl(); } ?> <!doctype html> <html xmlns:fb="http://www.facebook.com/2008/fbml"> <head> <title>php-sdk</title> <style> body { font-family: 'Lucida Grande', Verdana, Arial, sans-serif; } h1 a { text-decoration: none; color: #3b5998; } h1 a:hover { text-decoration: underline; } </style> </head> <body> <h1>php-sdk</h1> <?php if ($user): ?> <a href="<?php echo $logoutUrl; ?>">Logout</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 ?> <h3>PHP Session</h3> <pre><?php print_r($_SESSION); ?></pre> <?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 ?> </body> </html>
I found some methods to solve the problem, here is the one:
This is the method used in the example file with SDK
$logoutUrl = $facebook->getLogoutUrl(); <a href="<?php echo $logoutUrl; ?>">Logout</a>
This doesn’t destroy sessions stored in browser.
I modified the logout link as
<a href="?logout=yes">Logout</a>
it means, we change the line
$logoutUrl = $facebook->getLogoutUrl();
to
$logoutUrl = "?logout=yes";
And i add more code:
if(isset($_GET['logout'])){ if($_GET['logout']=='yes'){ $redir_url='http://'.$_SERVER['SERVER_NAME'].'/path/'; $logoutUrl = $facebook->getLogoutUrl(array('next'=>$redir_url)); session_destroy(); header('location:'.$logoutUrl); } }
This really works for me.