I am logging in and out of a website with facebook account information. For this, I made my code as follows
For Index.html my code is similar to this
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : 'XXXXXXXXXXXXXXXX',
channelURL : '',
status : true,
cookie : true,
oauth : true,
xfbml : false
});
};
function login(){
FB.getLoginStatus(function(r){
if(r.status === 'connected'){
window.location.href = 'fbconnect.php';
}else{
FB.login(function(response) {
if(response.authResponse) {
window.location.href = 'fbconnect.php';
} else {
}
},{scope:'email'});
}
});
}
(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 my code looks like
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'cookie' => true,
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl();
$logoutUrl = $facebook->getLogoutUrl();
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>';
echo '</pre>';
}
else{
echo 'not logged in ';
}
echo "<a href='logout.php'>log out </a>"
?>
Just like the logout system, so the user can also log out of the site, I made code for logout.php, like this
<?php
require 'src/facebook.php';
$token = $facebook->getAccessToken();
$url = 'https://www.facebook.com/logout.php?next=' . YOUR_SITE_URL .
'&access_token='.$token;
session_destroy();
header('Location: '.$url);
?>
But its error is shown here, for example
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 he easily enters a login, but when he logs out he shows an error. So can someone kindly tell me how to solve this problem? I am really new to facebook app. Therefore, any help and suggestions will be really noticeable. Thanks