我使用Facebook's PHP SDK登錄用戶到我的網站,像這樣:使用PHP或JavaScript Facebook SDK,我如何讓用戶登錄到我的網站?
<?php
$facebook = new Facebook(array(
"appId" => "myAppID",
"secret" => "mySecret",
));
$user = $facebook->getUser();
if ($user) {
try {
// user is authenticated
$user_profile = $facebook->api("/me");
} catch (FacebookApiException $ex) {
$user = null;
}
}
if ($user) {
$logoutURL = $facebook->getLogoutUrl();
?>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture" />
<a href="<?php echo($logoutURL); ?>">Logout</a>
<pre><?php print_r($user_profile); ?></pre>
<?php
} else {
?>
<a href="<?php echo($loginURL); ?>">Login</a>
<?php
}
?>
的問題是,當用戶返回到現場,他們不再登錄,並且需要重新登錄英寸
返回用戶是否可以保持登錄狀態?
更新:
我的精確解最終被JavaScript和PHP Facebook的軟件開發工具包的組合。
問題是cookie不用於存儲Facebook登錄信息 - PHP會話是(PHP會話cookie在瀏覽器關閉時會過期)。除此之外,除非您有離線訪問權限,否則訪問令牌將過期。換句話說,即使您將Facebook訪問令牌存儲在cookie中,它最終也會過期並且用戶將被註銷。
中的JavaScript我最終使用:
FB.init({
appId: "myAppID",
cookie: true, // enable cookies to allow the server to access the session
xfbml: true, // parse XFBML
oauth: true // enable OAuth 2.0
});
FB.Event.subscribe("auth.login", function(response) {
// a user logged in who was not previously logged in
window.location.refresh(true); // ideally modify the page with JS rather than refreshing
});
FB.Event.subscribe("auth.logout", function(response) {
// a user logged out who was previously logged in
window.location.refresh(true); // ideally modify the page with JS rather than refreshing
});
// check if the user is logged in or not, using the Facebook JS SDK
FB.getLoginStatus(null);
當加載頁面時,看到了Facebook的JavaScript SDK檢查與Facebook用戶是否登錄如果用戶登錄,和餅乾尚未設置,它將被設置。然後,您可以刷新頁面(或者,理想情況下,使用JavaScript修改它),服務器將能夠使用常規PHP SDK(以及此問題中的第一段代碼)訪問cookie。
這是我在跨瀏覽會話進行永久登錄時找到的最佳方式。
這是非常有用的,非常感謝你。 –
對於任何人在將來閱讀:在我的問題的第二部分有更多的信息來實現這一點。 –