I am getting an interesting SDK exception message from FB\'s PHP SDK. I set up my code exactly as the FB docs say to do..?
DEBUG ERROR MESSAGE:
[12-J
Put session_start();
on the TOP of your file
To expand Chancho's answer, the SDK will check to see if a session is active before it tries to store any data to it. There are 3 ways around this.
If you're just using PHP's native sessions, Chancho's answer will suffice. You have to make sure that session_start();
is at the very top of your script before any output is sent.
If you're using a web framework like Codeigniter, Yii, CakePHP, Laravel, etc, they usually have their own ways of storing a session. To overwrite the default session store, you'll need to write your own class that extends the \Facebook\FacebookRedirectLoginHelper
class. Then you'll need to overwrite the storeState()
and loadState()
methods. These are the only two methods in the whole SDK that interface with the $_SESSION
variable.
You can see a laravel implementation example in the GitHub issues for the SDK. Here's the laravel implementation:
class MyFacebookRedirectLoginHelper extends \Facebook\FacebookRedirectLoginHelper
{
protected function storeState($state)
{
Session::put('state', $state);
}
protected function loadState()
{
return $this->state = Session::get('state');
}
}
If you're trying to use the SDK in a stateless environment like the command line, there's no point in starting a session or overwriting how the session data is stored. You can simply disable the check with FacebookRedirectLoginHelper::disableSessionStatusCheck()
.
To disable the session check just run this.
$helper = new FacebookRedirectLoginHelper('*******');
$helper->disableSessionStatusCheck();
$session = $helper->getSessionFromRedirect();
// . . .
I hope that helps!
For using CakePHP v3.1.6 + Facebook SDK v4.1.
In the newer SDK, \Facebook\FacebookRedirectLoginHelper class has been changed to use \Facebook\FacebookSessionPersistentDataHandler class to handle session "state". Putting $this->request->session()->start() in AppController initialize will fix the problem using Facebook SDK with AuthComponent.
public function initialize() {
parent::initialize();
$this->request->session()->start();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => ['username' => 'email', 'password' => 'password']
]
],
'loginAction' => ['controller' => 'Users', 'action' => 'login']
]);
}