Facebook登录上的重定向页面的问题

时间:2020-04-07 15:23:23

标签: mysql facebook authentication redirect

我正在尝试在我的网站上使用Facebook登录名,并在数据库中插入Facebook用户数据。 我有那些文件,如下所示: index.php:

    session_start(); 
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css/preload.css?ve=5"/>
    <title>Myexpect</title>
 </head>
  <body>
  <?php
      if ($_SESSION['FBID']):
      require_once('connections/connect.php');
      ?>      <!--  After user login  -->
<div id="warper">
    <div class="logo">
    <img  width="150px" height="150px"src="images/Logo - Copy.png">
    </div>
    <div class="welcome">
        <h2>Welcome to our website</h2>
    </div>
    <div class="preload">
        <img  width="50" height="50px"src="loading.gif">
    </div>
</div>
    <?php   
    echo '<meta http-equiv = "refresh" content = "3; url = games.php" />'
?>

    <?php else: ?>     <!-- Before login --> 
        <div class="container">
            <div><a href="fbconfig.php"><img src="images/fb-login-btn.png" ></a></div>
        </div>
    <?php endif ?>

  </body>

</html>

fbconfig.php:

<?php
session_start();
// added in v4.0.0
require_once 'autoload.php';
require 'dbconfig.php';

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// init app with app id and secret
FacebookSession::setDefaultApplication( 'xxxxxxxxxxxx','xxxxxxxxxxxxxx' );
// login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper('https://xxxxx/fbconfig.php' );
try {
  $session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
  // When Facebook returns an error
} catch( Exception $ex ) {
  // When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
  // graph api request for user data
  $request = new FacebookRequest( $session, 'GET', '/me' );
  $response = $request->execute();
  // get response
  $graphObject = $response->getGraphObject();
        $fbid = $graphObject->getProperty('id');              // To Get Facebook ID
        $fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
        $femail = $graphObject->getProperty('email');    // To Get Facebook email ID
    /* ---- Session Variables -----*/
        $_SESSION['FBID'] = $fbid;           
        $_SESSION['FULLNAME'] = $fbfullname;
        $_SESSION['EMAIL'] =  $femail;


    $check = mysqli_query($connection,"select * from Users where Fuid='$fbid'");
    $check = mysqli_num_rows($check);
    if (empty($check)) { // if new user . Insert a new record       
    $query = "INSERT INTO Users (Fuid,Ffname,Femail) VALUES ('$fbid','$fbfullname','$femail')";
    mysqli_query($connection,$query);   
    } else {   // If Returned user . update the user record     
    $query = "UPDATE Users SET Ffname='$ffname', Femail='$femail' where Fuid='$fuid'";
    mysqli_query($connection,$query);

    /* ---- header location after session ----*/
  header("Location: index.php");
} } else {
  $loginUrl = $helper->getLoginUrl();
 header("Location: ".$loginUrl);
}
?>

我遇到的问题是,当用户尝试成功登录时,它不是重定向到index.php文件,而是使用代码fbconfig.php:

https://xxxx/fbconfig.php?code=AQAAuanQBQ9lmXsAgbCuRB3aYy3YEEhrd81VBMMjih5oHo4dK_C7zMkQPZnuX5EdVvRJ2v4ybyAQ3EZ7qTzIrK9Oo-uY0KWiA6ZjNVh_4I6J7_AZDsJ12f7LGfc8EAGwgAfWwAm2Scwkx0UON9Vpjj75SBg7TX7n....etc

当我单击返回时,它可以正常工作,而index.php可以工作 我该如何解决

1 个答案:

答案 0 :(得分:0)

对不起,这是变量名错误 该代码是错误的:

$query = "UPDATE Users SET Ffname='$ffname', Femail='$femail' where Fuid='$fuid'";

我将其更改为:

$query = "UPDATE Users SET Ffname='$fbfullname', Femail='$femail' where Fuid='$fbid'";

现在可以正常工作