繁体   English   中英

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

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

我成功使用Facebook SDK(PHP)将用户连接到我的网站,但我们在验证他们的帐户时遇到问题。 他们的帐户已成功通过身份验证,但出于某种原因,我的网站的会话已被清除。

流:

  • 用户登录我的站点(本地用户名和密码)
  • 用户通过弹出窗口连接到Facebook
  • Facebook验证用户并返回我的网站
  • 我的网站会话现在无效(在弹出窗口和主窗口中)导致用户注销

我正在使用Facebook SDK(PHP),我的网站使用CakePHP框架

任何帮助将不胜感激。

我不能告诉你什么是删除你的会话,但你可能想尝试这个(适合我)

使用Javascript SDK显示将打开弹出窗口以连接到FB的登录按钮

将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>

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;
        }
}

主要的想法是..在js中我调用ajax来检查fb会话然后将其保存在蛋糕会话中,并且js将刷新页面

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

我无法找出会话重置的原因,因此决定不使用SDK进行身份验证。 这就是我用来代替的。

$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