Javascript 登录Facebook并在所有子页面上启动会话

Javascript 登录Facebook并在所有子页面上启动会话,javascript,php,jquery,facebook-login,Javascript,Php,Jquery,Facebook Login,我正在尝试将Facebook登录添加到我的网站。我添加了以下代码: window.fbAsyncInit = function() { FB.init({ appId : 'XXX', xfbml : true, version : 'v2.4' }); }; (function(d, s, id){ var js, fjs = d.getElementsByTagName(s)[0];

我正在尝试将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'));
// 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 + '!';
 });
 }
我把这个放在打开身体标签之后

然后我把这个代码:

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'));
// 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>
现在我有了登录的代码。若我点击登录按钮,它会打开新窗口并询问我用户名和密码。登录控制台后,显示:

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>

登录


那么,我怎样才能用脸来做呢?

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

您在哪里创建了会话['username']我在signin.php文件中创建了会话,该文件通过ajax调用,以检查数据库中是否有用户名和密码。如果启动了true会话。如果代码对您有帮助,我可以提供代码。如果您创建了会话,那么该会话是否在其他页面上不可访问?我可以选择在注册页面中输入用户名和密码,然后将其插入数据库并启动会话,以便我可以在其他页面上访问它。现在我添加了face登录按钮,所以您不需要注册,只需使用face acc登录即可。当有人使用face登录时,我无法在其他页面上设置会话。