Facebook : You are not logged in: You are not logged in. Please log in and try again : <p>code throws error</p>

<pre><code>        $helper = $fb-&gt;getRedirectLoginHelper();
        $loginUrl = $helper-&gt;getLoginUrl("https://apps.facebook.com/{$appname}/", $permissions);
        echo "&lt;script&gt;window.top.location.href='".$loginUrl."'&lt;/script&gt;";
</code></pre>

<p>Error </p>

<blockquote>
  <p>You are not logged in: You are not logged in. Please log in and try again.</p>
</blockquote>

<p>the url which throws the error is (replaced my appname with appname) :</p>

<blockquote>
  <p><a href="https://www.facebook.com/v2.7/dialog/oauth?client_id=8651003434372244&amp;state=f2ad3183f9f04355435434534776ae19688ac&amp;response_type=code&amp;sdk=php-sdk-5.3.1&amp;redirect_uri=https%3A%2F%2Fapps.facebook.com%2Fappname%2F&amp;scope=email" rel="noreferrer">https://www.facebook.com/v2.7/dialog/oauth?client_id=8651003434372244&amp;state=f2ad3183f9f04355435434534776ae19688ac&amp;response_type=code&amp;sdk=php-sdk-5.3.1&amp;redirect_uri=https%3A%2F%2Fapps.facebook.com%2Fappname%2F&amp;scope=email</a></p>
</blockquote>

<p>full script</p>

<pre><code>     &lt;?php
    require_once  '../../Facebook/autoload.php';
    $fb = new Facebook\Facebook([
      'app_id' =&gt; "$appid",
      'app_secret' =&gt; "$appsecret",
      'default_graph_version' =&gt; 'v2.7',
    ]);
    $helper = $fb-&gt;getCanvasHelper();
    $permissions = ['email']; // optionnal
    try {

            $accessToken = $helper-&gt;getAccessToken();

    } catch(Facebook\Exceptions\FacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e-&gt;getMessage();
        exit;
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e-&gt;getMessage();
        exit;
     }
    if (isset($accessToken)) {

            $accessToken = (string) $accessToken;
            $fb-&gt;setDefaultAccessToken($accessToken);

        if (isset($_GET['code'])) {
            header('Location: ./');
        }

        // validating the access token
        try {
            $request = $fb-&gt;get('/me');
        } catch(Facebook\Exceptions\FacebookResponseException $e) {
            // When Graph returns an error
            if ($e-&gt;getCode() == 190) {
                $helper = $fb-&gt;getRedirectLoginHelper();
                $loginUrl = $helper-&gt;getLoginUrl("https://apps.facebook.com/{$appname}/", $permissions);
                echo "&lt;script&gt;window.top.location.href='".$loginUrl."'&lt;/script&gt;";
                exit;
            }
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
            // When validation fails or other local issues
            echo 'Facebook SDK returned an error: ' . $e-&gt;getMessage();
            exit;
        }
        // getting basic info about user
        try {
            $profile_request = $fb-&gt;get('/me?fields=name,first_name,last_name,email');
            $user_profile = $profile_request-&gt;getGraphNode()-&gt;asArray();
        } catch(Facebook\Exceptions\FacebookResponseException $e) {
            // When Graph returns an error
            echo 'Graph returned an error: ' . $e-&gt;getMessage();
            $url = "https://apps.facebook.com/{$appname}/";
            echo '&lt;script&gt;window.top.location.href='.$url.'&lt;/script&gt;';
            exit;
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
            // When validation fails or other local issues
            echo 'Facebook SDK returned an error: ' . $e-&gt;getMessage();
            exit;
        }
        // priting basic info about user on the screen
        //print_r($user_profile);
        // Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']
    } else {

        $helper = $fb-&gt;getRedirectLoginHelper();
        $loginUrl = $helper-&gt;getLoginUrl("https://apps.facebook.com/{$appname}/", $permissions);
        echo "&lt;script&gt;window.top.location.href='".$loginUrl."'&lt;/script&gt;";
    }
</code></pre>
