php - Facebook redirect after login not working
<p>I checked all the Stackoverflow previous question related to this. But none of them are same like mine.
I implemented Facebook login for my website. Its working good. But the problem is after login, the page is redirecting to Facebook home instead of redirecting to my website.</p>

<p>My Codes are below. I did not changed much from the official documentation of Facebook developers.</p>

<p>login.php</p>

<pre><code>&lt;?php

session_start();
require_once __DIR__ . '/Facebook/autoload.php';
$fb = new Facebook\Facebook([
  'app_id' =&gt; '****',
  'app_secret' =&gt; '****',
  'default_graph_version' =&gt; 'v2.2',
  ]);

$helper = $fb-&gt;getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$loginUrl = $helper-&gt;getLoginUrl('http://example.com/login-callback.php', $permissions);

echo '&lt;a href="' . $loginUrl . '"&gt;Log in with Facebook!&lt;/a&gt;';

?&gt; 
</code></pre>

<p>login-callback.php</p>

<pre><code>&lt;?php

# login-callback.php
session_start();
require_once __DIR__ . '/Facebook/autoload.php';
$fb = new Facebook\Facebook([
  'app_id' =&gt; '****',
  'app_secret' =&gt; '****',
  'default_graph_version' =&gt; 'v2.2',
  ]);

$redirectloc = $_SERVER['HTTP_REFERER'];  
$helper = $fb-&gt;getRedirectLoginHelper();
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)) {
  // Logged in!
  $_SESSION['facebook_access_token'] = (string) $accessToken;
  header("Location:$redirectloc"); 
exit();
  // Now you can redirect to another page and use the
  // access token from $_SESSION['facebook_access_token']
} elseif ($helper-&gt;getError()) {
  // The user denied the request
  exit;
}

?&gt;
</code></pre>

<p>When I am logged in already in my Facebook account from the same browser, and trying to login from my website, its working perfectly.
i.e <code>header("Location:$redirectloc");</code>  in login-callback.php is working as expected. Its redirecting to login.php.</p>

<p>Now If I logout from my Facebook account from the browser, and try to login from my website, its asking for Facebook id and password which is normal.</p>

<p>But after entering user id and password its redirecting to the Facebook home page instead of redirecting to login.php.</p>