After I login in my application using facebook sdk I am getting the facebook user id but after some time it automatically gets destroyed . This is happening frequently . I need to refresh the page every now and then . Can anyone help me to find the solution why this is happening ? I am using both javascript and php sdk . I have searched for the same but i have not got any proper solution .
Plz someone help me with this...
Here is the code I am using to login :
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $facebook->getAppID() ?>',
cookie: true,
xfbml: true,
oauth: true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(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);
}());
function loginfb()
{
FB.login(function(response) {
if (response.authResponse) {
//alert('Success!');
} else {
//alert('Login Failed!');
}
}, {scope: 'manage_pages,publish_actions,email'});
}
(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/all.js#xfbml=1&appId=<?php echo APP_ID_KEY; ?>";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
and here is the php code which i am using to get the facebook user id :
require_once('facebook.php');
$facebook = new Facebook(array(
'appId' => APP_ID_KEY,
'secret' => APP_SECRET,
));
$site_url = CALLBACK_URL;
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$friends = $facebook->api('/me/friends');
$token=$facebook->getAccessToken();
} catch (FacebookApiException $e) {
error_log($e);`enter code her`enter code here`e`
$user = null;
}
}
Now when I login for the first time I am getting the facebook user id but after some time automatically it becomes zero so i need to click on login button again to get the user id this is happening very frequently and its very annoying..