views:

5112

answers:

2

I am trying to build a Connect app using PHP and Zend Framework. I also have a Zend_Auth based user authentication system. Now, I am able to log in using Facebook but log out is not working.

I need to clear the Zend_Auth identity as well as remove all Facebook login info as well. What would be the best way to do this?

I tried facebook_client->expire_session() and facebook_client->clear_cookie_state(); together and also facebook_client->logout($next) after calling Zend_Auth::getInstance()->clearIdentity()

None of them seem to work.

+8  A: 

You have to call the javascript client logout first, then send them to your php logout script. So, call .js:

FB.Connect.logoutAndRedirect("/path/to/zend/logout/controller");

You'll see a modal popup that says "you are logging out of this site and facebook* You'll be redirected to wherever your logout script is:

try
{
    $facebook->expire_session();
}
catch (FacebookRestClientException $e)
{
    //you'll want to catch this
    //it fails all the time
}

I usually also call this function in the PHP logout script, just to be safe:

/**
 * Remove the local Facebook cookies that get set manually
 * 
 * @return void
 */
protected function _killFacebookCookies()
{
    // get your api key
    $apiKey = $this->getConfig()->getApiKey();
    // get name of the cookie
    $cookie = $this->getConfig()->getCookieName();

    $cookies = array('user', 'session_key', 'expires', 'ss');
    foreach ($cookies as $name) 
    {
        setcookie($apiKey . '_' . $name, false, time() - 3600);
        unset($_COOKIE[$apiKey . '_' . $name]);
    }

    setcookie($apiKey, false, time() - 3600);
    unset($_COOKIE[$apiKey]);       
}
Typeoneerror
Thanks! It works! :)
Abhinav
yeap, works! Tested it without the additional function _killFacebookCookies(), works fine.
Alex
A: 

In this case i think that the $cookie var it's useless. you should remove it from the source.

Andrés Cristi