duandiao3961 2014-10-10 06:40
浏览 93

登录facebook后,会在facebook的permision确认后继续重定向

I am applying "Login with facebook" on my website.

When I click Login with facebook.... It redirects to facebook asking for permission. I click ok. Now after that it keeps redirecting to facebook and my website simultaneously.

I am providing my code below. Can anyone help me finding out where is the mistake that is redirecting ?

Here is my Code:

index.php

<?php
session_start(); 

if ($_SESSION['FBID']): ?>      <!--  After user login  -->

<h1>Hello <?php echo $_SESSION['USERNAME']; ?></h1>

<li><?php echo $_SESSION['FULLNAME']; ?></li>

<div><a href="logout.php">Logout</a></div>

<?php else: ?>     <!-- Before login --> 

Not Connected
<div><a href="fbconfig.php">Login with Facebook</a></div>

<?php endif ?> 

fbconfig.php

<?php
require 'src/facebook.php';  // Include facebook SDK file
//require 'functions.php';  // Include functions
$facebook = new Facebook(array(
  'appId'  => 'APP_ID',   // Facebook App ID 
  'secret' => 'SECRET',  // Facebook App Secret
  'cookie' => true, 
));
$user = $facebook->getUser();

if ($user) {
  try {
    $user_profile = $facebook->api('/me');
        $fbid = $user_profile['id'];                 // To Get Facebook ID
        $fbuname = $user_profile['username'];  // To Get Facebook Username
        $fbfullname = $user_profile['name']; // To Get Facebook full name
        $femail = $user_profile['email'];    // To Get Facebook email ID
    /* ---- Session Variables -----*/
        $_SESSION['FBID'] = $fbid;           
        $_SESSION['USERNAME'] = $fbuname;
            $_SESSION['FULLNAME'] = $fbfullname;
        $_SESSION['EMAIL'] =  $femail;
    //       checkuser($fbid,$fbuname,$fbfullname,$femail);    // To update local DB
  } catch (FacebookApiException $e) {
    error_log($e);
   $user = null;
  }
}
if ($user) {
    header("Location: index.php");
} else {
 $loginUrl = $facebook->getLoginUrl(array(
        'scope'     => 'email', // Permissions to request from the user
        ));
 header("Location: ".$loginUrl);
}
?>
  • 写回答

1条回答 默认 最新

  • dowy77780 2014-10-10 07:06
    关注

    You need to provide redirect_url(URL of index.php)

    if ($user) {
        header("Location: index.php");
    } else {
     $loginUrl = $facebook->getLoginUrl(array(
            'scope'     => 'email', // Permissions to request from the user
            'redirect_uri' => 'URL of index.php'
            ));
     header("Location: ".$loginUrl);
    }
    

    One more thing, you should not disclose your app_id and app_secret. Remove it asap.

    评论

报告相同问题?