title: How to check if user is logged in with new facebook php api
body: <p>I am migrating my old FB app to the new graph API using the <a href="http://github.com/facebook/php-sdk/" rel="nofollow noreferrer">PHP API</a></p>
  
  <p>I have two pages: public ones (which require no user login) and private ones (which do)</p>
  
  <p>So the code of every single php page in my application works as follows:</p>
  
  <pre><code>if (this_page_requires_user_login){
        $facebook = new Facebook(...) ;
        $session = $facebook-&gt;getSession ;
        if (!$session){
                $url =$facebook.getLoginUrl(array(next =&gt; current_page );
                echo "&lt;fb:redirect url=$url/&gt;" ;
  
  }
  // the rest of the code continues here
  </code></pre>
  
  <p>Now as you can see, this way of working forwards every single page to the login url and while it works, it is also slow and appends the &amp;session={bla} string to very single url.</p>
  
  <p>I need a way to check where a user is already logged in before I redirect him to loginpage. But i can not find such method in the php api. What's the best way to do this?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This seemed to do the trick</p>
  
  <pre><code>if ($session) {
    try {
      $me = $facebook-&gt;api('/me');
      if ($me) {
         // here comes your code for user who is logged in
      }
    } catch (FacebookApiException $e) {
      login()
    }
  }else{
   login()
  }
  
  function login(){
  
    $url =$facebook.getLoginUrl(array(next =&gt; current_page );
  
  
        echo "&lt;fb:redirect url=$url/&gt;" ;
  }
  </code></pre>