gpt4 book ai didi

javascript - 使用 Facebook 登录并在所有子页面上启动 session

转载 作者:行者123 更新时间:2023-11-30 00:24:42 26 4
gpt4 key购买 nike

我正在尝试将 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 is not defined就是这一行:

 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>

那我怎么用脸做呢?

最佳答案

你也必须在你这端保持 session 。 Facebook 不会为您的应用创建任何 session 。通过 FB 登录后,您将获得用户详细信息。根据您的需要保存这些详细信息,并在连接确认后创建一个 session ,每当用户使用 FB 检查该用户的记录时,如果数据存在,则创建 session ,否则添加新数据,然后创建 session 。

关于javascript - 使用 Facebook 登录并在所有子页面上启动 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31871191/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com