title: Facebook login has become unstable
body: <p>I have been using Facebook login (PHP SDK) for a website for months without problems.
  Since a few days, we are experiencing problems.</p>
  
  <p>Here is the code</p>
  
  <pre><code>    $fbconfig['appid' ]     = "...";
      $fbconfig['secret']     = "...";
      $fbconfig['baseurl']    = "myurl/index.php";
  
      $facebook = new Facebook(array(
        'appId'  =&gt; $fbconfig['appid'],
        'secret' =&gt; $fbconfig['secret'],
        'cookie' =&gt; true,
      ));
  
      $user       = $facebook-&gt;getUser();
  
      $loginUrl   = $facebook-&gt;getLoginUrl(
              array(
                  'scope'         =&gt; 'email,user_likes,user_birthday',
                  'redirect_uri'  =&gt; $fbconfig['baseurl']
              )
      );
  
      $logoutUrl  = $facebook-&gt;getLogoutUrl(
              array(
                  'next'  =&gt; $fbconfig['baseurl'].'?f=logout'
              )
      );
      if ($user) {
       ....try {
                  $fb_user_infos_ar = $facebook-&gt;api('/me');
          }
  
          catch (FacebookApiException $e) {
  
              $facebook-&gt;destroySession();
              $user = null;
          }
      }
  
  if (isset($fb_user_infos_ar)){
  do something...
  }
  else{
  show facebook login button
  }
  </code></pre>
  
  <p>The problem we are experiencing is that the login seems to work, but after a while (most of the time even after a few seconds) the user is not recognized anymore as logged into facebook ($user is 0 and the login button is showed).</p>
  
  <p>Has there been any change in the last few days to facebook rules or browsers that can explain this behavior? I have tried with both firefox and chrome with the same result.
  Since my PHP sdk was a few months old, I also tried with the very latest one with the same result. I also noticed that in the last version examples, "base_url" is not mentioned anymore, do you know why?</p>
  
  <p><strong>EDIT AFTER COMMENTS</strong></p>
  
  <p>After some hints coming from the comments, I have tried to debug what is happening; it seems that the token doesn't become invalid; in fact when I log-in again I still get a token generated hours before.</p>
  
  <p>For some reason, however, sometime the user is considered NOT LOGGED (basically $facebook->getUser() is false) even if he is LOGGED.</p>
  
  <p>One of the reason, as complex857 suggested, could be that facebook is having problems in talking with my site; in the last days I actually noticed a general performances loss in the site and after a debugging session today, I can say that the performances loss appears when there is a facebook activity involved.
  In particular I measured the execution time of this single instruction: </p>
  
  <pre><code>$fb_user_infos_ar = $facebook-&gt;api('/me');
  </code></pre>
  
  <p>and it is about 5 seconds; I have no idea how long it took before but I guess is too much; what do you think?</p>
  
  <p>About possible cookies problem, what should I check?</p>
  
  <p><strong>EDIT 2 after answer</strong></p>
  
  <p>More details: if there are problems during the API call (catch part) I used to destroy the facebook session with</p>
  
  <pre><code>$facebook-&gt;destroySession();
  </code></pre>
  
  <p>and that's why the user logged out. Sometime it is actually not needed to destroy the session (e.g. if there is a timeout due to a network problem) so I should handle the exception according to the error.</p>
  
  <p>As far as I have understood, the piece of code that Andy Jones provided</p>
  
  <pre><code>echo "error code = " . $e-&gt;getCode() . " error = " . $e-&gt;getMessage() . "\n";
  </code></pre>
  
  <p>works fine for a CURL error but not for other kind of errors (e.g. token expired). For example if I print out the exception when the token is invalid due to password change I get:</p>
  
  <blockquote>
    <p>FacebookApiException Object (
        [result:protected] => Array
            (
                [error] => Array
                    (
                        [message] => Error validating access token: The session has been invalidated because the user has changed the
    password.
                        [type] => OAuthException
                        [code] => 190
                        [error_subcode] => 460
                    )</p>
  
  <pre><code>    )
  
  [message:protected] =&gt; Error validating access token: The session has been invalidated because the user has changed the password.
  [string:Exception:private] =&gt; 
  [code:protected] =&gt; 0 ....
  </code></pre>
  </blockquote>
  
  <p>$e->getCode() just give me 0 instead of 190.</p>
  
  <p>Here is the list of the errors: <a href="https://developers.facebook.com/docs/reference/api/errors/" rel="nofollow">https://developers.facebook.com/docs/reference/api/errors/</a> I think that a simple way to handle the process in my specific case would be: destroy the facebook session if the code is 190 or 102, otherwise try to repeat the operation. What do you think and which would be the best practice piece of code to handle this in a general way?</p>
  
  <p>Another question: while logging the exceptions with $e->getMessage() I got
  "Error validating access token: The session has been invalidated because the user has changed the password."
  but I'm sure that for the user I was using at the time (my own) I haven't changed the facebook password. How can this happen? Is the same message produced even in other cases (e.g. when a token is at its natural expiration date)?</p>
  
  <p>Thanks. </p>