PHP / Programmings

Facebook PHP SDK Logout Issue Solved

When working with Facebook login integration using facebook-php SDK, the really annoying thing is the issue with logout. The logout method used in the example file, included with sdk download package, is redirecting us to a facebook logout url. But it doesn’t logout, since session variables still remains there.

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.And this method didn’t work for me.

I modified the logout link as

<a href="?logout=true">Logout</a>

And the php code as

if(isset($_GET['logout'])){
    if($_GET['logout']=='true'){
     	$redir_url='http://'.$_SERVER['SERVER_NAME'];
	$logoutUrl = $facebook->getLogoutUrl(array('next'=>$redir_url));
        session_destroy();
	header('location:'.$logoutUrl);     
    }
}

This really works for me. If you also face this problem this may help you.

Advertisements

9 thoughts on “Facebook PHP SDK Logout Issue Solved

  1. you can put php code on same php file or separate php file or in controller if you follow MVC structure. This is code is part of a php sdk which have logout issue. this is not full code. you can find full code on facebook developer site. please see this link. https://developers.facebook.com/docs/php/gettingstarted/
    You can download SDK from GitHub https://github.com/facebook/facebook-php-sdk..

    refer javascript sdk too.. That might be easier..
    Hope this will help.. 🙂

  2. just 1 small thing for the other guys:

    We use this:

    <a href=”?logout=true” rel=”nofollow” > Logout </a>

    instead of this:

    <a href=”?logout=yes” rel=”nofollow” > Logout </a>

    Small thing, but cause headaches 🙂

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s