3

starting from a thinkdiff.net tutorial I have built a simple test page (local env) with a facebook login/logout link.If logged in i want to echo out the fb user API.

Im using the latest facebook PHP SDK (v.2.1.2).

It seems to work but when i logout I receive this exception:

FacebookApiException Object
(
[result:protected] => Array
    (
        [error] => Array
            (
                [type] => OAuthException
                [message] => Error validating access token: The session is invalid because the user logged out or because auth.expireSession was invoked.
            )

    )

[message:protected] => Error validating access token: The session is invalid because the user logged out or because auth.expireSession was invoked.
[string:Exception:private] => 
[code:protected] => 0
[file:protected] => C:\wamp\www\fb\facebook.php
[line:protected] => 543
[trace:Exception:private] => Array
    (
        [0] => Array
            (
                [function] => _graph
                [class] => Facebook
                [type] => ->
                [args] => Array
                    (
                        [0] => /me
                    )

            )

        [1] => Array
            (
                [file] => C:\wamp\www\fb\facebook.php
                [line] => 492
                [function] => call_user_func_array
                [args] => Array
                    (
                        [0] => Array
                            (
                                [0] => Facebook Object
                                    (
                                        [appId:protected] => 1819654718*****
                                        [apiSecret:protected] => a2fccb8e93638b50c8d6b2**********
                                        [session:protected] => 
                                        [signedRequest:protected] => 
                                        [sessionLoaded:protected] => 1
                                        [cookieSupport:protected] => 1
                                        [baseDomain:protected] => 
                                        [fileUploadSupport:protected] => 
                                    )

                                [1] => _graph
                            )

                        [1] => Array
                            (
                                [0] => /me
                            )

                    )

            )

        [2] => Array
            (
                [file] => C:\wamp\www\fb\fb.php
                [line] => 33
                [function] => api
                [class] => Facebook
                [type] => ->
                [args] => Array
                    (
                        [0] => /me
                    )

            )

    )

[previous:Exception:private] => 
)

this is my test page code

$fbconfig['appid']  = "18196**********";     
$fbconfig['api']  = "5c6910be575e4e688ac6d**********";     
$fbconfig['secret']  = "a2fccb8e93638b50c8d6b2**********"; 

try
{         
   include_once "facebook.php"; 
}     
catch(Exception $o)
{         
 echo '<pre>';         
 print_r($o);         
 echo '</pre>';     
}     
// Create our Application instance.     
$facebook = new Facebook(array('appId'  => $fbconfig['appid'],'secret' =>    $fbconfig['secret'],'cookie' => true));       

$session = $facebook->getSession();       
$fbme = null;     
// Session based graph API call.     
if (!empty($session)) 
{       
  d($session);
  try 
  {         
    $uid = $facebook->getUser();         
    $fbme = $facebook->api('/me');       
  } 
  catch (FacebookApiException $e) 
  {           
    d($e);       
  }    
}      
function d($d)
{         
echo '<pre>';         
print_r($d);         
echo '</pre>';     
} 


if ($fbme) 
{   
  $logoutUrl = $facebook->getLogoutUrl(); 
  echo"<a href='{$logoutUrl}'>logout</a>";
  d($fbme);
} 
else 
{   
  $loginUrl = $facebook->getLoginUrl(array('req_perms' => 'email,read_stream,user_birthday')); 
  echo"<a href='{$loginUrl}'>login</a>";
}

thanks

Luca

2 Answers 2

5

I would try manually clearing the session on logout. Put a GET parameter on the return url, or use a different return url, and then do this:

$facebook->destroySession();

The problem is that the session cookie persists even after the user is logged out. When the user returns to your page you are trying to use that expired session to make requests. Good luck.

Sign up to request clarification or add additional context in comments.

5 Comments

yeah but why this issue does not happen in the tutorial??I was wondering =) maybe depending on wrong settings?
That is a year old tutorial, the platform has changed a lot since then. They use fbml for the logout button, not the PHP SDK, and fbml is deprecated now, it's unreliable. From what I can tell the code you wrote here is your code, only based on the tutorial.
Ok.but do you think mine would be the right way for logging in and out a fb user(setting the session to null) or I should do something else?=)
..by the way what is SDK standing for?? =)
There really is no right way, personal preference and circumstances play a large part. Here is a better recommended tutorial for you to get started with benbiddington.wordpress.com/2010/04/23/…
1

Nice answer, You must call destroySession() first before

facebook->destroySession();

then you could call the getLoginUrl()

1 Comment

This works with the latest API as of 10/05/2012. $facebook->setSession(null); Does not work anymore. "setSession()" is not a method.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.