Php Facebook服务器端登录问题
因此,正如标题所示,我正在使用Facebook php sdk来授权我网站上的用户,我遇到的问题是,在登录几分钟后,我刷新了页面$user=$Facebook->getUser();已消失,页面认为我已注销,但如果我再次刷新,它将再次被授权 index.phpPhp Facebook服务器端登录问题,php,facebook,api,authentication,sdk,Php,Facebook,Api,Authentication,Sdk,因此,正如标题所示,我正在使用Facebook php sdk来授权我网站上的用户,我遇到的问题是,在登录几分钟后,我刷新了页面$user=$Facebook->getUser();已消失,页面认为我已注销,但如果我再次刷新,它将再次被授权 index.php <?php session_start(); include_once "facebook/fbaccess.php"; ?> <div id="mainMenu"> <?php if ($user) {
<?php
session_start();
include_once "facebook/fbaccess.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>
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,
));
}
?>
检查会话生存期设置和cookie。当你持续浏览你的网站或只是在一段不活动的时间之后,是否存在相同的行为?你不应该显示应用程序秒。我认为问题将来自你的会话结束,而不是FACEBOOK。。尝试增加运行此应用程序的服务器的会话时间。我不认为这是会话,但我也不确定它是什么。正如我之前所说的,我只需要刷新twise就可以让它工作,所以我猜第二次刷新时,它会确认我已登录facebook并再次授权。