Question

I am doing a login and logout in a website with facebook account info. For that I have made my code like this

For Index.html my code is like this

<div id="fb-root"></div>
<script type="text/javascript">
//<![CDATA[
window.fbAsyncInit = function() {
   FB.init({
     appId      : 'XXXXXXXXXXXXXXXX', // App ID
     channelURL : '', // Channel File, not required so leave empty
     status     : true, // check login status
     cookie     : true, // enable cookies to allow the server to access the session
     oauth      : true, // enable OAuth 2.0
     xfbml      : false  // parse XFBML
   });
};
// logs the user in the application and facebook
function login(){
FB.getLoginStatus(function(r){
     if(r.status === 'connected'){
            window.location.href = 'fbconnect.php';
     }else{
        FB.login(function(response) {
                if(response.authResponse) {
              //if (response.perms)
                    window.location.href = 'fbconnect.php';
            } else {
              // user is not logged in
            }
     },{scope:'email'}); // which data to access from user profile
 }
});
}

// Load the SDK Asynchronously
(function() {
   var e = document.createElement('script'); e.async = true;
   e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
   document.getElementById('fb-root').appendChild(e);
}());

//]]>
</script>
<a href='#' onclick='login();'>Facebook Login</a>

and in fbconnect.php I have my code looks like this

<?php
    require 'src/facebook.php';
    $facebook = new Facebook(array(
           'appId'  => 'XXXXXXXXXXXXXXXX',
           'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
           'cookie' => true,
    ));
    $user = $facebook->getUser();
    $loginUrl = $facebook->getLoginUrl();
    //echo "<a href='$loginUrl'>login</a>";

    $logoutUrl = $facebook->getLogoutUrl();
    //echo $loginUrl; 
    if($user){
        $_SESSION['user_info'] = $user; 
        $_SESSION['user_pro']= $facebook->api('/me');
        echo $_SESSION['user_pro']['first_name'];
        echo $_SESSION['user_pro']['last_name'];
        echo '<pre>';
        //print_r($_SESSION);
        echo '</pre>';
    }
    else{
        echo 'not logged in '; 
    }
    echo "<a href='logout.php'>log out </a>"
?>

As I also want logout system so that a user can also logout from the site I made the code for logout.php like this

<?php
require 'src/facebook.php';  //including the facebook php sdk
$token = $facebook->getAccessToken();
$url = 'https://www.facebook.com/logout.php?next=' . YOUR_SITE_URL .
  '&access_token='.$token;
session_destroy();
header('Location: '.$url);
?>

But here its showing error like

Notice: Undefined variable: facebook in logout.php on line 3 Fatal error: Call to a member function getAccessToken() on a non-object in logout.php on line 3.

Here it is doing login easily but in logout it is showing the error. So can someone kindly tell me how to solve this issue? I am really newbie to facebook application. So any help and suggestions will be really appreciable. Thanks

No correct solution

OTHER TIPS

Try like

require 'src/facebook.php';
$facebook = new Facebook();  //including the facebook php sdk
$token = $facebook->getAccessToken();

Where $facebook is the instance for the Facebook class.And as per your comments if you want to logout from facebook also then you need to destroy the session at your logout function like

$facebook->destroySession();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top