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

Php 当用户授权网站时,如何使用facebook的范围功能数据,php,sql,facebook,Php,Sql,Facebook,我在我的网站上有一个facebook登录,当你点击它时,它会弹出一个弹出窗口,用户登录facebook,如果他们没有授权我的网站,它就会这样做。我知道你可以使用Facebook scope功能请求数据,但我如何获取这些数据并将其存储在数据库中,以便保存他们的电子邮件地址等。我有一个使用Facebook的注册功能,可以将数据保存到我的数据库中,但我想知道是否可以通过这种方式简化登录和授权?如果是这样,我该怎么做?提前感谢您可以使用会话 首先在此处下载Facebook PHP SDK: 将faceb

我在我的网站上有一个facebook登录,当你点击它时,它会弹出一个弹出窗口,用户登录facebook,如果他们没有授权我的网站,它就会这样做。我知道你可以使用Facebook scope功能请求数据,但我如何获取这些数据并将其存储在数据库中,以便保存他们的电子邮件地址等。我有一个使用Facebook的注册功能,可以将数据保存到我的数据库中,但我想知道是否可以通过这种方式简化登录和授权?如果是这样,我该怎么做?提前感谢

您可以使用会话

首先在此处下载Facebook PHP SDK:

将facebook.php和base_facebook.php放入文件夹“facebook”

将dbconfig.php放入文件夹“config”。资料来源:


文件夹“config”中的functions.php。资料来源:


logout.php源代码:

<?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
}
?>

示例用法login.php。资料来源:

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

?>

您已经在数据库中保存了哪些数据?您的表使用了哪些属性和属性?
创建表(如果不存在)
users`(
id
int(11)NOT NULL自动递增,
email
varchar(70)COLLATE utf8\u unicode\u ci默认为NULL,
oauth\u uid
varchar(200)校对utf8\u unicode\u ci默认为空,
username
varchar(100)校对utf8\u unicode\u ci默认为空,主键(
id
)引擎=MyISAM默认字符集=utf8校对=utf8\u unicode\u ci自动增量=4`我一直收到这样的错误警告:无法修改标题信息-在login.php第36行中dbconfig.php已经发送了标题-知道如何修复此错误吗?事实上,不用担心,我已经对其进行了排序。谢谢你的帮助。这就像一个charmquick问题,如果登录成功,如何让login.php重定向?
<?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
}
?>
<?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);
}
?>
<?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>';

?>