因此,正如标题所暗示的那样,我正在使用 Facebook php sdk 在我的网站上授权用户,我遇到的问题是,在登录几分钟后,我刷新页面 $user = $facebook->getUser() ; 消失了,页面认为我已注销,但如果我再次刷新,它会再次被授权。
索引.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,
));
}
?>
索引.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-接口/