2

I am trying to add Facebook login to my site. I added this code:

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'));

I put this just after opening body tag.

Then I put this code:

// 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 + '!';
 });
 }

and finally login button:

<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>

 <div id="status">
 </div>

On document load in console it says: Uncaught ReferenceError: FB is not defined that is this line:

 FB.getLoginStatus(function(response) {
 statusChangeCallback(response);
 });

Now I have code for log in. If I click on login button it opens new windows and ask me a username and password. After login in console it says:

statusChangeCallback
(index):100 Object {status: "connected", authResponse: Object}
(index):141 Welcome!  Fetching your information.... 
(index):143 Successful login for: Name Surname

and above login button it says: Thanks for logging in, Name Surname!

Problem is that if I go on another subpage www.example.com/page2 I am not logged in. For example I made login trough site and if you sign in I put:

<?php
session_start();
if (isset($_SESSION['username'])) {
?>
<p>logged in</p>

so how can I do it with face ?

4
  • 1
    where have you created $_SESSION['username']
    – Vinie
    Aug 7, 2015 at 8:57
  • I created session in signin.php file which is called via ajax to check if is username and password in database. If true session is started. I can provide code if it helps you. Aug 7, 2015 at 9:05
  • if you created session then is that session is not accessible at other pages?
    – Vinie
    Aug 7, 2015 at 9:09
  • I have options that you enter your username and password in sign up page and than it is inserted in database and session is started so I can access it on other pages. Now I added face login button so you don't need to sign up, just log in with your face acc. And I can't set session on other pages when someone logs in with face. Aug 7, 2015 at 9:18

1 Answer 1

1

You have to maintain session at your end also. Facebook doesn't create any session for your app. On login through FB you will get user details. Save those details according to your need and create a session after connected acknowledgment and whenever user login with FB check records for that user if data exists then create session otherwise add new data and then create session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.