简体   繁体   中英

PHP/JavaScript Facebook Login Redirect loop

I'm using the PHP SDK for Facebook to login and the first time it logs in fine but then if you return to the application it get's stuck in a redirect loop.

The flow of the app is as follows, user navigates to an app on facebook page tab, if the do not like the page they see a like us page, if not they get redirected to app.php where the following code exists.

<?php
opcache_reset();
require 'sdk/facebook.php';  // Include facebook SDK file
$facebook = new Facebook(array(
  'appId'  => 'XXXXXX',   // Facebook App ID 
  'secret' => 'XXXXXXXXXXXXXX',  // Facebook App Secret
  'cookie' => true,    
));
$user = $facebook->getUser();
if ($user) {
  try {
    $user_profile = $facebook->api('/me');
    session_start();
          $fbid = $user_profile['id'];           // To Get Facebook ID

  } catch (FacebookApiException $e) {
    error_log($e);
   $user = null;
  }
}
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl(array(
         'next' => 'http:/localhost:8888/logout.php',  // Logout URL full path
        ));
} else {
 $loginUrl = $facebook->getLoginUrl(array(
        'scope'        => 'user_birthday', // Permissions to request from the user
        ));
}
?>
<!DOCTYPE html>
... html stuff
<?php if ($user): ?>
show the goods
<?php else: ?>
redirect to login     
<script>window.top.location.href = '<?php echo $loginUrl; ?>'; </script>
<?php endif ?>
... rest of html page

I've read numerous people having similar issues but yet to find a solid response as to what is happening and why. At this stage none of the JavaScript SDK is being used yet.

Any help would be greatly appreciated!

Add this code after you get user data

 if(!empty($user)) 
{ 
  if(!$user)
{
  exit();
}
  // YOUR FUNCTION
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM