title: Facebook PHP SDK - require login on specific account
body: <p>I'm tired of digging through tons of tutorials/documentations which don't help me at all. </p>
  
  <p>What I have now (everything is placed inside admin control panel):</p>
  
  <ol>
  <li>If user is logged on correct account (administrator of page with granted rights), everything works fine, post on page is posted as impersonated site. </li>
  <li>If he is logged on other account, nothing happens. Site redirects him to his wall. </li>
  <li>If he isn't logged on any account, he's redirected to facebook login - if he logs onto correct account, he returns to acp (it's bad solution, because it'll clear his form)</li>
  </ol>
  
  <p>I want to achieve:</p>
  
  <ol>
  <li>If logged in, everything as it was</li>
  <li>Else popup with login to specific (correct) account</li>
  </ol>
  
  <p>At the moment I'm using only PHP, but solution with JS is permitted. </p>
  
  <p>My code:</p>
  
  <pre><code>&lt;?php
  /*(...)*/
  $facebook = new Facebook(array(
        'appId'  =&gt; $apiid,
        'secret' =&gt; $secret,
        'cookie' =&gt; true,
      ));
  
      $session = $facebook-&gt;getSession();
      $me = null;
      if ($session) {
          try {
              $uid = $facebook-&gt;getUser();
              $me = $facebook-&gt;api('/me');
          } catch (FacebookApiException $e) {
              error_log($e);
          }
  
          if($me) {
  
  
              //In order to post to the page later on we need to generate an Access Token for that page, to do this we get me-accounts in the following api call
              $accounts = $facebook-&gt;api('/me/accounts');
  
              //Loop through the array off accounts to find the page with a matching ID to the one we need
              foreach($accounts['data'] as $account){
                if($account['id'] == PAGEID){
                  $ACCESS_TOKEN = $account['access_token'];
                  }
              }
          }
  
          $message=$data['facebook_text'];
          $attachment = array(
              'message' =&gt; $data['facebook_text'],
              'name' =&gt; $data['name'],
              'description' =&gt; '',
              'link'=&gt;$someurl,
              'access_token' =&gt; $ACCESS_TOKEN
          );
          if($image_url != NULL) $attachment['picture'] = $image_url;
  
          try {
              if($facebook-&gt;api('/PAGEID/feed', 'post', $attachment))
              {
                  //other stuff
              }
          } catch (FacebookApiException $e) {
              error_log($e);
              //other stuff
          }
      }
      else
      {
          $login_url = $facebook-&gt;getLoginUrl();
          header("Location: $login_url");
          exit;
      }
  /* (...) */
  ?&gt;
  </code></pre>
  
  <p>Solution can't redirect anywhere, because it's inside form, so all data'll be lost.</p>