[英]Facebook login integration to website on localhost
My index.php file include this: 我的index.php文件包括:
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function () {
FB.init({
appId: '<?php echo $appId; ?>',
cookie: true,
xfbml: true,
channelUrl: '<?php echo $return_url; ?>channel.php',
oauth: true
});
};
(function () {
var e = document.createElement('script');
e.async = true;
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
function CallAfterLogin() {
FB.login(function (response) {
if (response.status === "connected") {
LodingAnimate(); //Animate login
FB.api('/me', function (data) {
if (data.email == null) {
//Facbeook user email is empty, you can check something like this.
alert("You must allow us to access your email id!");
ResetAnimate();
} else {
AjaxResponse();
}
});
}
}, {
scope: '<?php echo $fbPermissions; ?>'
});
}
//functions
function AjaxResponse() {
//Load data from the server and place the returned HTML into the matched element using jQuery Load().
$("#results").load("process_facebook.php");
}
//Show loading Image
function LodingAnimate() {
$("#LoginButton").hide(); //hide login button once user authorize the application
$("#results").html('<img src="img/ajax-loader.gif" /> Please Wait Connecting...');
}
//Reset User button
function ResetAnimate() {
$("#LoginButton").show(); //Show login button
$("#results").html(''); //reset element html
}
</script>
process_facebook.php include
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH'])&&strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' )
{
//initialize facebook sdk
$facebook = new Facebook(array(
'appId' = > $appId,
'secret' = > $appSecret, ));
$fbuser = $facebook - > getUser();
if ($fbuser) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$me = $facebook - > api('/me'); //user
$uid = $facebook - > getUser();
} catch (FacebookApiException $e) {
//echo error_log($e);
$fbuser = null;
}
}
// redirect user to facebook login page if empty data or fresh login requires
if (!$fbuser) {
$loginUrl = $facebook - > getLoginUrl(array('redirect_uri' = > $return_url, false));
header('Location: '.$loginUrl);
}
//user details
$fullname = $me['first_name'].' '.$me['last_name'];
$email = $me['email'];
/* connect to mysql using mysqli */
$mysqli = new mysqli($hostname, $db_username, $db_password, $db_name);
if ($mysqli - > connect_error) {
die('Error : ('.$mysqli - > connect_errno.') '.$mysqli - > connect_error);
}
//Check user id in our database
$UserCount = $mysqli - > query("SELECT COUNT(id) as usercount FROM usertable WHERE fbid=$uid") - > fetch_object() - > usercount;
After this it will just check from database and insert and update. 之后,它将仅从数据库中进行检查并进行插入和更新。 Still all step by step is followed in this blog: http://www.sanwebe.com/2012/05/ajax-facebook-connect-with-jquery-php 在此博客中仍然遵循所有步骤: http : //www.sanwebe.com/2012/05/ajax-facebook-connect-with-jquery-php
I am facing a problem. 我面临一个问题。 When I am login from facebook it just keep on loading no further thing happen ie user data are not able to display. 当我从Facebook登录时,它只会继续加载,不会再发生任何事情,即用户数据无法显示。 so please provide me solution so that i can display and store facebook user data. 所以请提供给我解决方案,以便我可以显示和存储Facebook用户数据。
In the App Settings in the App Dashboard, you can write the Site URL as- 在“应用程序信息中心”的“应用程序设置”中,您可以将网站网址写为-
http://localhost/HarshTest/
Then in you app (ie /HarshTest/index.php
), you can try the facebook login and it will work. 然后,在您的应用程序(即/HarshTest/index.php
)中,您可以尝试使用facebook登录,它将成功。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.