dqgo99177 2014-12-16 16:22
浏览 76

Facebook API身份验证存储在SESSION中的PHP令牌

I am trying to authenticate users by logging in in their facebook account. I am able to get an access token which I save on my $_SESSION variable and get the information I need. My problem is that, when I go to facebook and log in with a different account, the information that I get is from the first account. The application in this case is supposed to ask me to log in again (since I logged out from my facebook and logged in as a different user) but instead it is not and returns info about the first account. I guess my problem is that I store the token in my session which is the same the next time but how do I solve this?

$app_id = '...';
$app_secret = '....';
$redirect_url='...';

FacebookSession::setDefaultApplication($app_id, $app_secret);

// Define Facebook's login helper and redirect back to our page.
$helper = new FacebookRedirectLoginHelper( $redirect_url );

// Check to ensure our session was started correctly and the access token exists.
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
  // Using the access token, create a new session.
  $session = new FacebookSession( $_SESSION['fb_token'] );

  // Determine if the defined session is indeed valid.
  if ( !$session->validate() ) {
    $session = null;
  }
}
// Check if an active session exists.
if ( !isset( $session ) || $session === null ) {
 // If no session exists, let's try to create one.
 $session = $helper->getSessionFromRedirect();
 }

// Make sure we have a session started.
if ( isset( $session ) ) {
    // Save the session
    $_SESSION['fb_token'] = $session->getToken();

   // Create a new Facebook session using our token.
   $session = new FacebookSession( $session->getToken() );
   echo 'Connected to Facebook!';
  } else {
   // Show login url
        echo '<a href="' . $helper->getLoginUrl( array( 'email', 'user_friends') ) .     '">Login</a>';
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)