title: Facebook API: Login using JavaScript SDK then checking login state with PHP
body: <p>I'm using Facebook's JavaScript SDK to bring up a Login Popup when the user clicks the Login button.</p>
  
  <p>The code is, as Facebook provides in the doucmentation:</p>
  
  <pre><code>$(".loginButton").click(function(){
   FB.login(function(response) {
      FB.api('/me', function(response) {
         console.log(response.id);
         //User ID shows up so I can see that the user has accepted the app.
       });
   });
  </code></pre>
  
  <p>I can also use <code>FB.getLoginStatus()</code> to check that the user has indeed logged in and accepted the application.</p>
  
  <p>However, now I would like to execute a function with PHP. As far as I understand, PHP has <code>$user</code> which is assigned the UserID after a successful login.</p>
  
  <p>The problem is after the JS Login <code>$user</code> is still <code>0</code>. I'm stuck and I can't figure out why it won't assign the correct user ID to <code>$user</code></p>
  
  <p>Here's my JS:</p>
  
  <pre><code>    &lt;script type="text/javascript"&gt;  
  
          window.fbAsyncInit = function() {
              FB.init({
                  appId  : '&lt;?php echo $AppId; ?&gt;',
                  status : true, // check login status
                  cookie : true, // enable cookies to allow the server to access the session
                  xfbml  : true,
                  channelUrl  : '&lt;?php echo $ServerPath; ?&gt;channel.php' // custom channel
              });
  
      }; 
  
  
      //Get Login Status
      function amILoggedIn(){
          var toreturn = "";
          FB.getLoginStatus(function(response) {
              if (response.status === 'connected') {
                  window.userId =  response.authResponse.userID;
              toreturn = "GetLoginStatus: logged in" + " " + "&lt;?php echo $user; ?&gt;";
              } else {
              toreturn = "GetLoginStatus: Logged Out";
              }
              console.log(toreturn);
          }); 
  
  
      };
  
  
      // Load the SDK asynchronously
      (function(d, s, id){
           var js, fjs = d.getElementsByTagName(s)[0];
           if (d.getElementById(id)) {return;}
           js = d.createElement(s); js.id = id;
           js.src = "//connect.facebook.net/en_US/all.js";
           fjs.parentNode.insertBefore(js, fjs);
         }(document, 'script', 'facebook-jssdk'));
  
              var obj = {
                      method: 'feed',
                      link: 'http://www.google.com'
                  };
  
  
      $(".loginPopupButton").click(function(){
           FB.login(function(response) {
              FB.api('/me', function(response) { //user-specific stuff
                 console.log(response.id);
               });
           });
  
      });
  
      &lt;/script&gt;
  </code></pre>
  
  <p>And here's the PHP:</p>
  
  <pre><code>    &lt;?php
      include_once('fbapi/facebook.php');
      include_once('fbapi/Authenticated_User.php');
      include_once('config.php');
  
  
      // Create our Application instance.
      $facebook = new Facebook(array(
                      'appId'  =&gt; $AppId,
                      'secret' =&gt; $AppSecret
                  ));
      //Facebook Authentication part
      $user = $facebook-&gt;getUser();
  
  
      $perms = "manage_pages";
      if(isset($_GET['backlink']))
          $redirectUrl   = urldecode($_GET['backlink']);
      else
          $redirectUrl   = $fullserverpath;
  
      $cancelUrl   = $ServerPath."index.php?page=loginWFb";
  
      //AA - where to go to get FB popup.    
      $loginUrl = $facebook-&gt;getLoginUrl(
              array(
                  'scope' =&gt; $perms,
                  'redirect_uri' =&gt; $redirectUrl,
                  'cancel_uri' =&gt; $cancelUrl
              )
      );
  
  
  
      //AA- Defining that a powerUSER is someone who's logged in
      if(isset($user)){
          $_SESSION['loggedIn'] = $user;
  
      }
  
      ?&gt;
  </code></pre>