0

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

4

1 回答 1

0

您可以使用会话。

首先在这里下载 Facebook PHP SDK:https ://github.com/facebook/php-sdk

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

将 dbconfig.php 放在文件夹“config”中。资源:

<?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());

?>

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

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

?>

文件夹“config”中的 fbconfig.php。资源:

<?php

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

?>

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

示例用法 home.php。资源:

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

?>

使用您自己的详细信息编辑 fbconfig.php 和 dbconfig.php。这是我自己文件的复制/粘贴,但它们会让您了解如何通过 facebook 验证和登录用户,并使用 facebook 提供的信息创建用户。

祝你好运!

于 2012-05-05T22:53:04.617 回答