登录Facebook并在所有子页面上启动会话
Log in with Facebook and start a session on all subpages
我正在尝试添加Facebook登录到我的网站。我添加了以下代码:
window.fbAsyncInit = function() {
FB.init({
appId : 'XXX',
xfbml : true,
version : 'v2.4'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
我把它放在body标签后面。
然后输入以下代码:
// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
testAPI();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}
// This function is called when someone finishes with the Login
// Button. See the onlogin handler attached to it in the sample
// code below.
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
// Here we run a very simple test of the Graph API after login is
// successful. See statusChangeCallback() for when this call is made.
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
});
}
和最后登录按钮:
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>
<div id="status">
</div>
在控制台加载文档时,它说:Uncaught ReferenceError: FB未定义就是这一行:
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
现在我有了登录的代码。如果我点击登录按钮,它会打开一个新窗口,询问我用户名和密码。登录控制台后显示:
statusChangeCallback
(index):100 Object {status: "connected", authResponse: Object}
(index):141 Welcome! Fetching your information....
(index):143 Successful login for: Name Surname
和上面的登录按钮上面写着:感谢您登录,姓名!
问题是,如果我去另一个子页面www.example.com/page2我没有登录。例如,我通过网站登录如果你登录,我输入:
<?php
session_start();
if (isset($_SESSION['username'])) {
?>
<p>logged in</p>
那么我该如何设置face呢
您还必须在您的终端维护会话。Facebook不会为你的应用创建任何会话。通过Facebook登录后,你将获得用户详细信息。根据需要保存这些详细信息,并在连接确认后创建会话,每当用户登录FB检查该用户的记录时,如果数据存在,则创建会话,否则添加新数据,然后创建会话。
相关文章:
- 我的jQuery插件参数没有正确启动,遇到了问题
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- 从控制器返回后Ajax启动事件激发
- Meteor上的启动页面
- 如何防止网页加载后自动启动功能
- 如何从JavaScriptInterface启动Navigation Drawer
- 我的 JS 代码中的会话启动事件
- 量角器给出“无法启动 Web 驱动程序会话”错误
- 在单击时启动 asp 会话(单击链接时)
- 如何在Javascript中启动php会话
- 是否可以在浏览器中启动多个js调试器独立会话
- Angular 2——在guard启动之前初始化会话服务
- 检查会话是否已经启动,并设置javascript要使用的全局变量
- Ajax——通过onload和onunload启动和结束会话
- AngularJS启动会话
- 登录Facebook并在所有子页面上启动会话
- 在服务器重新启动期间保持passport会话有效
- 如何使用ajax启动跳棋游戏会话
- 在Windows 8 WinJS应用程序中启动应用程序是否有唯一的会话ID
- 如何在iframe中启动一个有效的用户会话