当用户授权网站时,如何使用Facebook的范围功能数据

i have a facebook login on my website and when you click it it launches a pop up where the user signs into Facebook and if they haven't authorised my site it does that. I know you can request data using Facebook scope function but how do I take this data and store it in a database so I can save their email address etc. I have a register function using Facebook which saves the data into my database but I was wondering if I could streamline login and authorisation this way? if so how would I do it? thanks in advance

You can work with sessions.

First download Facebook PHP SDK here: https://github.com/facebook/php-sdk

Put facebook.php and base_facebook.php in the folder "facebook"

Put dbconfig.php in folder "config". Source:

<?php

define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'db_name');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());

?>

functions.php in the folder "config". Source:

<?php

require 'dbconfig.php';

class User {
    function checkUser($uid, $username, $email) 
    {
        $query = mysql_query("SELECT * FROM `users` WHERE oauth_uid = '$uid'") or die(mysql_error());
        $result = mysql_fetch_array($query);
        if (!empty($result)) {
            # User is already present
        } else {
            #user not present. Insert a new Record
            $query = mysql_query("INSERT INTO `users` (id, oauth_uid, username, email) VALUES ('', $uid, '$username', '$email')") or die(mysql_error());
            $query = mysql_query("SELECT * FROM `users` WHERE oauth_uid = '$uid'");
            $result = mysql_fetch_array($query);
            return $result;
        }
        return $result;
    }
}

?>

fbconfig.php in folder "config". Source:

<?php

define('APP_ID', 'xxxxxxxxxxxxxx');
define('APP_SECRET', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

?>

logout.php source:

<?php

if (array_key_exists("logout", $_GET)) {
    session_start();
    unset($_SESSION['id']);
    unset($_SESSION['username']);
    session_destroy();
    header("location: home.php"); // or anywhere where you want them to redirect them to
}
?>

Example usage login.php. Source:

<?php

require 'facebook/facebook.php';
require 'config/fbconfig.php';
require 'config/functions.php';

$facebook = new Facebook(array(
            'appId' => APP_ID,
            'secret' => APP_SECRET,
            ));

$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
  }
    if (!empty($user_profile )) {
        # User info ok? Let's print it (Here we will be adding the login and registering routines)

        $username = $user_profile['name'];
        $uid = $user_profile['id'];
        $email = $user_profile['email'];
        $user = new User();
        $userdata = $user->checkUser($uid, $username, $email);
        if(!empty($userdata)){
            session_start();
            $_SESSION['id'] = $userdata['id'];
            $_SESSION['oauth_id'] = $uid;
            $_SESSION['username'] = $userdata['username'];
            $_SESSION['email'] = $userdata['email'];
            header("Location: home.php");
        }
    } else {
        # For testing purposes, if there was an error, let's kill the script
        die("There was an error.");
    }
} else {
    # There's no active session, let's generate one
    $login_url = $facebook->getLoginUrl(array( 'scope' => 'email')); // you can add more scopes
    header("Location: " . $login_url);
}
?>

Example usage home.php. Source:

<?php

//Always place this code at the top of the Page
session_start();
if (!isset($_SESSION['id'])) {
    // Not logged in? Redirect to main page
    header("location: index.php");
}

echo '<h1>Welcome</h1>';
echo 'id : ' . $_SESSION['id'];
echo '<br /><img src="https://graph.facebook.com/' . $_SESSION['oauth_id'] . '/picture?type=large">';
echo '<br />Name : ' . $_SESSION['username'] . ' (' . $_SESSION['oauth_id'] . ')' ;
echo '<br />Email : ' . $_SESSION['email'];
echo '<br />You are login with : ' . $_SESSION['oauth_provider'];
echo '<br />Logout from <a href="logout.php?logout">' . $_SESSION['oauth_provider'] . '</a>';

?>

Edit fbconfig.php and dbconfig.php with your own details. This is a copy/paste from my own files, but they will give you an idea how you can authenticate and login users through facebook and create users with the info facebook provides.

Good luck!