title: Facebook PHP SDK - staying logged in
body: <p>I'm currently using the PHP Facebook SDK for my project (<a href="http://github.com/facebook/php-sdk/" rel="nofollow noreferrer">http://github.com/facebook/php-sdk/</a>). I have set it up so that a user will have to click a link to visit Facebook, which will then verify the user to use the application, and then return the session details to my site, which is fine. However, I want to store these session details within a database so that the user on logging in, can access information from their account at any time. Currently, if the user logs out of Facebook, and then tries to retrieve information from my site, I get the following exception "OAuthInvalidTokenException: Error processing access token." If the user is logged into Facebook on the same PC, then there isn't an issue.</p>
  
  <p>Is there anything that I have to set, or is there some other way I should be performing this connectivity to be able to retrieve the data when the user isn't logged in?</p>
  
  <p>The code I use to get/set the Facebook session is:</p>
  
  <pre><code>require 'libraries/facebook/facebook.php';
  
  // Create our Application instance (replace this with your appId and secret).
  $facebook = new Facebook(array(
    'appId'  =&gt; 'xxxx',
    'secret' =&gt; 'xxxx',
  ));
  
  $session = $facebook-&gt;getSession();
  
  $me = null;
  // Session based API call.
  if ($session) {
      //Save to database
  
    } catch (FacebookApiException $e) {
          error_log($e);
    }
  } else {
      $loginUrl = $facebook-&gt;getLoginUrl();
      header('Location: ' . $loginUrl);
  }
  </code></pre>
  
  <p>Once I have the session saved to the database from this part, I display the data by posting the database serialized session to this:</p>
  
  <pre><code>$session = unserialize($_POST['session']);
      require_once('libraries/facebook/facebook.php');
  
      $facebook = new Facebook(array(
        'appId'  =&gt; 'xxxx',
        'secret' =&gt; 'xxxx',
      ));
  
      $facebook-&gt;setSession($session, false);
  
      try {
          $uid = $facebook-&gt;getUser();
          // Do stuff
      } catch (FacebookApiException $e) {
          echo "&lt;tr&gt;&lt;td&gt;" . $e . "&lt;/td&gt;";
      }
  </code></pre>
  
  <p>Is there anything glaringly wrong with what I'm doing here? Or can I not use this method to create an oAuth connection that lets me request data for users when they are not logged in?</p>
  
  <p>Thanks,</p>
  
  <p>Dan</p>