I am working on website, in which i want integrate the facebook login with my website login system. I have integrated facebook-php-sdk in code. Login functionality work fine. Below are the points that I have issue…
- Once logged out and click on login the facebook login screen not open.
- Once logged out and refresh the page, the login deatils is displaying again. And login button disappears.
- If the above two points will solved then how to maintain login session through out the website?
For above 2 points I have used the below code.
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => YOUR_APP_ID,
'secret' => SECRET_KEY,
));
session_start();
if($_REQUEST['msg'] == 'logout'){
setcookie('fbsr_'.$facebook->getAppId(), '', (time() - 3600), '/', 'domain.com');
$sts = $facebook->destroySession();
session_destroy();
header("Location: index.php");
//echo '<meta http-equiv="refresh" content="2;url=index.php">';
}
$userId = $facebook->getUser();
if ($userId && !isset($_SESSION['fbdata'])) {
$_SESSION['fbdata'] = array("userid" => $userId);
}
<?php if ($userId) {
$userInfo = $facebook->api('/' + $userId); ?>
Welcome <?= $userInfo['name'] ?>
<?php } else { ?>
<div id="fb-root"></div>
<fb:login-button></fb:login-button>
<?php } ?>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?= YOUR_APP_ID ?>',
status : true,
cookie : true,
xfbml : true,
oauth : true,
});
FB.Event.subscribe('auth.login', function(response) {
//window.location.reload();
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
<br />
<br />
<a href="<?php echo $_SERVER['PHP_SELF']."?msg=logout"; ?>">Logout</a>
I found the answer on my questions below.
1.Once logged out and click on login the facebook login screen not open.
function removeCookie( name, path, domain) { if ( getCookie( name ) ) { document.cookie = name + '=' + ( ( path ) ? ';path=' + path : '') + ( ( domain ) ? ';domain=' + domain : '' ) + ';expires=Thu, 01-Jan-1970 00:00:01 GMT'; } }It works for me.
2.Once logged out and refresh the page, the login deatils is displaying again. And login button disappears.
3.If the above two points will solved then how to maintain login session through out the website?