我正在使用CodeIgniter内置的会话类,因此我不希望Facebook SDK启动它自己的会话(session_start()
并使用$_SESSION
变量)。
有没有办法阻止SDK使用本机会话?如果,我如何使它使用CodeIgniter会话类呢?它甚至可能吗?
答案 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 VERSION 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(),
// . . .
]);
}
我希望这有帮助!