我在我的网站上登录了facebook,当你点击它时会启动一个用户登录Facebook的弹出窗口,如果他们没有授权我的网站就这样做。我知道你可以使用Facebook范围功能请求数据,但我如何获取这些数据并将其存储在数据库中,以便我可以保存他们的电子邮件地址等。我有一个使用Facebook的注册功能,将数据保存到我的数据库但我想知道如果我可以通过这种方式简化登录和授权?如果是这样我怎么办?提前谢谢
答案 0 :(得分: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());
?>
functions.php在“config”文件夹中。来源:
<?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提供的信息创建用户。
祝你好运!