Facebook SDK + CodeIgniter sessions

感情迁移 提交于 2019-12-11 06:58:04

问题


I'm using CodeIgniter's built in session class, and therefore I don't want the Facebook SDK to start it's own session (by session_start() and using the $_SESSION variable).

Is there any way to prevent the SDK from using native sessions and if, how do I make it utilize the CodeIgniter session class instead? Is it even possible?


回答1:


This is pretty late but just in case someone else runs into the same issue. Just implement the PersistentDataHandler in a custom class as stated here: https://developers.facebook.com/docs/php/PersistentDataInterface/5.0.0

Here is a codeigniter version I implemented. (NOTE: Session library is autoloaded so I have omitted loading it. If that not your case endeavour to load it)

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

Then enable your custom class like this

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

NOTE (FOR CODEIGNITER VERSION 3 or less)

Don't forget to include the custom class and Facebook SDK wherever you decide to instatiate the Facebook class. See example below:

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(),
      // . . .
      ]);
}

I hope this helps!



来源:https://stackoverflow.com/questions/19349755/facebook-sdk-codeigniter-sessions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!