Friday, 2 January 2015

Session not active, could not store state Facebook SDK

It was session start problem, so you must start your session properly at the start of the page like this....

define('FACEBOOK_SDK_V4_SRC_DIR','../Vendor/fb/src/Facebook/');
require_once("../Vendor/fb/autoload.php");
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

FacebookSession::setDefaultApplication(FACEBOOK_APP_ID, FACEBOOK_APP_SECRET);
$helper = new FacebookRedirectLoginHelper(FACEBOOK_REDIRECT_URI);
$this->set('fb_url', $helper->getLoginUrl());



You may also refer following tutorial facebook oAuth login using php.

Facebook OAuth 2 Login Using PHP

If you start a session that's already started you will get php notice error that's session already started. so It always better idea to check a session before you are going to start, so starting your php session using following php script is always safe.
   if (session_status() == PHP_SESSION_NONE) {
       session_start(); 
   } 

If you are using CakePHP use session_start() at the start of the controller function .

public function login()
{
   if (session_status() == PHP_SESSION_NONE) {
    session_start(); 
   } 
   FacebookSession::setDefaultApplication(FACEBOOK_APP_ID, FACEBOOK_APP_SECRET);
   $helper = new FacebookRedirectLoginHelper(FACEBOOK_REDIRECT_URI);
   $this->set('fb_url', $helper->getLoginUrl());
}


No comments:

Post a Comment