简体   繁体   English

Facebook API SDK(PHP)清除网站会话

[英]Facebook API SDK (PHP) clearing site sessions

I am successfully using the Facebook SDK (PHP) to connect users to my site, but I'm having issues when they authenticate their account. 我成功使用Facebook SDK(PHP)将用户连接到我的网站,但我们在验证他们的帐户时遇到问题。 Their account is successfully authenticated, but for some reason my site's sessions are cleared. 他们的帐户已成功通过身份验证,但出于某种原因,我的网站的会话已被清除。

Flow: 流:

  • User logs into my site (local username and password) 用户登录我的站点(本地用户名和密码)
  • User connects to Facebook in a popup 用户通过弹出窗口连接到Facebook
  • Facebook authenticates user and returns back to my site Facebook验证用户并返回我的网站
  • My sites session is now invalid (both in the popup and main window) causing the user to be logged out 我的网站会话现在无效(在弹出窗口和主窗口中)导致用户注销

I am using the Facebook SDK (PHP) and my site uses the CakePHP framework 我正在使用Facebook SDK(PHP),我的网站使用CakePHP框架

Any help will be greatly appreciated. 任何帮助将不胜感激。

I can't tell you what is deleting your session, but you might want to try this (works for me) 我不能告诉你什么是删除你的会话,但你可能想尝试这个(适合我)

use the Javascript SDK to display the login buttons that will open the popup to connect to FB 使用Javascript SDK显示将打开弹出窗口以连接到FB的登录按钮

add the js SDK to your page like this: 将js SDK添加到您的页面,如下所示:

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({appId: '<?php echo FB_API_ID; ?>', status: true, cookie: true, xfbml: true});
    FB.Event.subscribe('auth.login', function() {
        new Request({
            'method': 'get',
            'url': '<?php echo $this->Html->url(array('controller'=>'users','action'=>'login_fb'));?>',
            'onSuccess': function(result){
                window.location.reload();       
            }
        }).send();
  });
  };
  (function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
      '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());
</script>

On the auth.login event i'm using an ajax call to /users/login_fb that will use the Facebook SDK to check the facebook session: auth.login事件中,我正在使用ajax调用/ users / login_fb,它将使用Facebook SDK检查facebook会话:

    App::import('Lib', 'facebook_sdk/facebook');
    // "MyAuth" is a custom Auth Component that extends the normal Auth component
    $this->MyAuth->facebook = new Facebook(array(
      'appId'  => FB_API_ID,
      'secret' => FB_SECRET,
      'cookie' => true,
    ));

    $me = null;
    $session = $this->MyAuth->facebook->getSession();
    if ($session) {
      try {
        $uid = $this->MyAuth->facebook->getUser();
        $me = $this->MyAuth->facebook->api('/me');
      } catch (FacebookApiException $e) {
        error_log($e);
      }
    }

    if ($me) {
        $this->Session->write('FbLogin.session',$session);
        $this->Session->write('FbLogin.user',$me);
        $UserModel = ClassRegistry::init('User');
        $user = $UserModel->findByUid($me['id']);
        if(!$user){
            $UserModel->create();
            $user_data = array( 'username'=>$me['username'],
                        'name'=>$me['first_name'],
                        'lastname'=>$me['last_name'],
                        'email'=>$me['email'],
                        'group_id'=>GROUP_VISITOR,
                        'uid'=>$me['id']
                        );
            $UserModel->save($user_data);
            $user['User']['id'] = $UserModel->id;
        } 
        $this->Session->write($this->MyAuth->sessionKey, $user['User']);
        $this->MyAuth->_loggedIn = true;
        }
}

the main idea is that.. in js i call an ajax to check the fb session and then save it in the cake session , and the js will refresh the page 主要的想法是..在js中我调用ajax来检查fb会话然后将其保存在蛋糕会话中,并且js将刷新页面

可能值得检查Cake安全级别,它可能正在进行引用检查(我认为它在“高”设置中执行此操作,也可能是“中等”),这将使会话无效。

I couldn't find out why the session was being reset so decided not to use the SDK for the authentication. 我无法找出会话重置的原因,因此决定不使用SDK进行身份验证。 This is what I used instead. 这就是我用来代替的。

$code = (isset ($_REQUEST['code']) ? $_REQUEST['code'] : null);

if (empty ($code)) {
    $dialogUrl = 'http://www.facebook.com/dialog/oauth?client_id=' . $this->appId . '&redirect_uri=' . urlencode($this->url) . '&scope=' . implode(',', $this->scope);
    header('Location: ' . $dialogUrl);
    die;
}
else {
    $tokenUrl = 'https://graph.facebook.com/oauth/access_token?client_id=' . $this->appId . '&redirect_uri=' . urlencode($this->url) . '&client_secret=' . $this->secret . '&code=' . $code;
    $accessToken = file_get_contents($tokenUrl);

    $this->Session->write('facebookAccessToken', $accessToken);

    $graphUrl = 'https://graph.facebook.com/me?' . $accessToken;
    $fbUser = json_decode(file_get_contents($graphUrl));

    if ($fbUser) {
        ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM