William Boman
William Boman

Reputation: 2227

Facebook SDK + CodeIgniter sessions

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?

Upvotes: 1

Views: 692

Answers (1)

noadek
noadek

Reputation: 21

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!

Upvotes: 1

Related Questions