2013-01-18 62 views
0

我有试过,现在修为天,而不能一个大问题。的FB.login(功能(响应)刷新页面时/我不更新

我得到了下面的代码登录在Facebook上的一个应用程序。除了当页面重新加载时(使用window.location.reload();)它不会更新API/me,所以它总是带着登录按钮回来

EXCEPTION:当我点击F5刷新页面,然后它运行良好

如果有人可以帮助,将不胜感激

感谢

<?php 

require 'facebook.php'; 

// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxx', 
'secret' => 'xxxxxxxxxxxxx',)); 

$user = $facebook->getUser(); 

// login or logout url will be needed depending on current user state. 
if ($user) { 
$logoutUrl = $facebook->getLogoutUrl(); 
} else { 
$loginUrl = $facebook->getLoginUrl(); 
} 

$user = $facebook->getUser(); 
// Session based API call. 
if ($user) { 
    try { 
    $me = $facebook->api('/me'); 
} catch (FacebookApiException $e) { 
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>'; 
    $user = null; 
} 
} 

?> 
<!DOCTYPE html> 
<html xmlns:fb="http://www.facebook.com/2008/fbml"> 
<head> 
<title>test full name and photo</title> 
</head> 
<body> 
<div id="fb-root"></div> 
<script> 
window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : '<?php echo $facebook->getAppId(); ?>', // App ID 
     channelURL : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File 
     status  : true, // check login status 
     cookie  : true, // enable cookies to allow the server to access the session 
     oauth  : true, // enable OAuth 2.0 
     xfbml  : true // parse XFBML 
    }); 

    // whenever the user logs in, we refresh the page 
    FB.Event.subscribe('auth.login', function() { 
     window.location.reload(); 
    }); 

}; 

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

function fbLogin() { 
     FB.login(function(response) { 
     if (response.status == 'connected') { 
       window.location.reload();   } 
     else { 
     alert('Try to connect'); 
     } 
    }, {scope: 'email,user_likes'}); 
    } 
    </script> 




<?php if ($user): ?> 
USER LOGGIN 

<?php else: ?> 
<div> 
Connexion button: <INPUT TYPE="BUTTON" ONCLICK="fbLogin()" value="login"> 
</div> 
<?php endif ?> 


<?php if ($user): ?> 
    <?php $uid = $me['id']; ?> 
    <div align="center"> 
     <img id="image" src="https://graph.facebook.com/<?php echo $uid; ?>/picture" /> 
     <div id="name"><?php echo $me['name']; ?></div> 
     <div id="name">UID: <?php echo $me['id']; ?></div> 
     <div id="name">Courriel <?php echo $me['email']; ?></div> 

    </div> 
<?php endif ?> 
</body> 
</html> 
+0

你试过window.location.reload(true); ? –

+0

是的,没有任何更改 – Vince

+0

您是否尝试过其他方法,例如: document.location.reload(true); ? 这里是一些其他的替代品页面:http://stackoverflow.com/questions/2624111/preferred-method-to-reload-page-with-javascript –

回答

1

FB.login回报response.status,这是由FB.getLoginStatus返回。有关更多信息,请参阅FB.login document

+0

谢谢,我会看看它。 我的网站: [Concours Quebec](http://www.concoursduquebec.com/index.php) – Vince