title: Check if connected to app, if not redirect to Login with Facebook PHP SDK / API
body: <p>I'm struggling to find a way to detect if users are not connected to my application, I want to send the activity 'View' an 'Item' which works perfectly fine, but they have to click a login link first, when just reloads the page and they are logged in.</p>
  
  <p>How can I make this happen automatically? How can you check if the user is connected to the app?
  I have tried the following code, but users who are not connected just get a blank page.</p>
  
  <pre><code>$facebook = new Facebook($config);
  $user_id = $facebook-&gt;getUser();
  
      // Check if logged in
      if($user_id) {
              $params = array(
                'ok_session' =&gt; 1, // Logged in and connected
                'no_user' =&gt; 2, // Logged out of facebook
                'no_session' =&gt; 3, // Logged in but not connected
              );
              $next_url = $facebook-&gt;getLoginStatusUrl($params);
  
              if($next_url == 2 || $next_url == 3){
                  header('Location: '.$facebook-&gt;getLoginUrl(array('scope' =&gt; 'publish_stream')));
              }
          } else {
              // Not logged in
              header('Location: '.$facebook-&gt;getLoginUrl(array('scope' =&gt; 'publish_stream')));
      } 
  </code></pre>
  
  <p>There doesn't seem to be a function that checks if the user is connected so I made use of the <code>getLoginUrl</code> function. </p>
  
  <p><code>$user_id</code> is supplied by <code>$user_id = $facebook-&gt;getUser();</code></p>