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

时间:2014-10-10 06:40:59

标签: php facebook facebook-graph-api

我在我的网站上应用“登录facebook”

当我点击登录Facebook时...它重定向到Facebook要求许可。我点击确定。之后,它会同时重定向到Facebook和我的网站。

我在下面提供我的代码。任何人都可以帮我找出重定向的错误在哪里?

这是我的代码:

的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 个答案:

答案 0 :(得分:0)

您需要提供redirect_url(index.php的URL)

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);
}

还有一件事,你不应该透露你的app_id和app_secret。尽快删除它。