我正在尝试添加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检查该用户的记录时,如果数据存在,则创建会话,否则添加新数据,然后创建会话。