Facebook SDK + CodeIgniter会话 [英] Facebook SDK + CodeIgniter sessions
问题描述
我使用的是CodeIgniter的内置会话类,因此,我不希望Facebook SDK开始自己的会话(通过 session_start()
并使用 $ _ SESSION )代码>变量.)
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).
有什么方法可以防止SDK使用本机会话,如果要如何使用它来代替CodeIgniter会话类?甚至有可能吗?
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?
推荐答案
这很晚了,但以防万一有人遇到相同问题.只需在自定义类中实现PersistentDataHandler,如下所示: https://developers.facebook.com/docs/php/PersistentDataInterface/5.0.0
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
这是我实现的codeigniter版本.(注意:会话库是自动加载的,因此我省略了它的加载.如果您的情况不尽如人意,请尝试加载它)
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(),
// . . .
]);
注意(适用于CODEIGNITER版本3或更低版本)
无论您决定实例化Facebook类,别忘了包括自定义类和Facebook SDK.请参见下面的示例:
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(),
// . . .
]);
}
我希望这会有所帮助!
这篇关于Facebook SDK + CodeIgniter会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!