タイトルが示すように、Facebook php sdk を使用して自分のサイトでユーザーを承認しています。問題は、ログインしてから数分後にページを更新して $user = $facebook->getUser() を実行することです。 ; がなくなり、ページはログアウトしたと見なされますが、再度更新すると再び承認されます。
index.php
<?php
session_start();
include_once "facebook/fbaccess.php";
?>
fbaccess.php
<?php
//Application Configurations
$app_id = "xxxxxxxxxxxxx";
$app_secret = "XXXXXXXXXXXXXXXXXXXXX";
$site_url = "http://xxxxxxxxx";
try{
include_once "facebook.php";
}catch(Exception $e){
error_log($e);
}
// Create our application instance
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based
// on whether the user is logged in.
// If we have a $user id here, it means we know
// the user is logged into
// Facebook, but we don’t know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
$facebook->setExtendedAccessToken();
$access_token = $facebook->getAccessToken();
if($user){
//==================== Single query method ======================================
try{
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$_SESSION['fid'] = $user_profile['id'];
}catch(FacebookApiException $e){
error_log($e);
$user = NULL;
}
//==================== Single query method ends =================================
}
if($user){
// Get logout URL
$logoutUrl = $facebook->getLogoutUrl();
}else{
// Get login URL
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream', 'offline_access',
'redirect_uri' => $site_url,
));
}
?>
index.php
<div id="mainMenu">
<?php if ($user) {echo '<a href="' . $logoutUrl . '"><h5>Logout</h5></a>'; }else{ echo '<a href="' . $loginUrl . '"><h5>Login</h5></a>';} ?>
</div>
ログイン プロセスにこのチュートリアルを使用しています: http://25labs.com/tutorial-integrate-facebook-connect-to-your-website-using-php-sdk-v-3-xx-which-uses-graph- API/