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
1条回答 默认 最新
- duanshan2988 2012-05-05 22:53关注
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!
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 微信会员卡等级和折扣规则
- ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
- ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
- ¥15 gdf格式的脑电数据如何处理matlab
- ¥20 重新写的代码替换了之后运行hbuliderx就这样了
- ¥100 监控抖音用户作品更新可以微信公众号提醒
- ¥15 UE5 如何可以不渲染HDRIBackdrop背景
- ¥70 2048小游戏毕设项目
- ¥20 mysql架构,按照姓名分表
- ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分