【问题标题】:Facebook SDK + CodeIgniter sessionsFacebook SDK + CodeIgniter 会话
【发布时间】:2013-10-13 20:12:41
【问题描述】:

我正在使用 CodeIgniter 的内置会话类,因此我不希望 Facebook SDK 启动它自己的会话(通过 session_start() 并使用 $_SESSION 变量)。

有什么方法可以阻止 SDK 使用本机会话,如果,我该如何让它使用 CodeIgniter 会话类?有可能吗?

【问题讨论】:

  • 好吧,您可以创建自己的(facebook.php 的)派生类,在其中您基本上用适当的 CI 调用替换所有标准会话处理? (在构造函数中,您可以首先使用 get_instance() 获取对 CI 控制器的引用,这似乎很常见)

标签: php codeigniter facebook-sdk-3.0


【解决方案1】:

这已经很晚了,但以防其他人遇到同样的问题。只需在自定义类中实现 PersistentDataHandler,如下所述:https://developers.facebook.com/docs/php/PersistentDataInterface/5.0.0

这是我实现的 codeigniter 版本。 (注意:会话库是自动加载的,所以我省略了加载它。如果不是你的情况,请尝试加载它)

use Facebook\PersistentData\PersistentDataInterface;

class CIPersistentDataHandler implements PersistentDataInterface
{
    public function __construct()
    {
       $this->ci =& get_instance();
    }
    /**
    * @var string Prefix to use for session variables.
    */
    protected $sessionPrefix = 'FBRLH_';

    /**
    * @inheritdoc
    */
    public function get($key)
    {
        return $this->ci->session->userdata($this->sessionPrefix.$key);
    }

    /**
    * @inheritdoc
    */
    public function set($key, $value)
    {
        $this->ci->session->set_userdata($this->sessionPrefix.$key, $value);
    }
}

然后像这样启用您的自定义类

$fb = new Facebook\Facebook([
  // . . .
  'persistent_data_handler' => new CIPersistentDataHandler(),
  // . . .
  ]);

注意(对于 CODEIGNITER 版本 3 或更低版本)

不要忘记在您决定实例化 Facebook 类的任何位置包含自定义类和 Facebook SDK。请参见下面的示例:

require_once APPPATH.'libraries/facebook-php-sdk/autoload.php';
require_once APPPATH.'libraries/CIPersistentDataHandler.php';

use Facebook\Facebook;
use Facebook\Authentication\AccessToken;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
use Facebook\Helpers\FacebookJavaScriptHelper;
use Facebook\Helpers\FacebookRedirectLoginHelper;

class Facebooklogin {
    ...

    $fb = new Facebook\Facebook([
      // . . .
      'persistent_data_handler' => new CIPersistentDataHandler(),
      // . . .
      ]);
}

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多