Facebook SDK error: Cross-site request forgery validation failed. Required param "state" missing from persistent data



PHP Snippet 1:

session_start();
include_once 'path/to/Facebook/autoload.php';

$fb = new \Facebook\Facebook([
    'app_id' => 'your_app_id',
    'app_secret' => 'your_secret_app_id',
    'default_graph_version' => 'v2.10'
]);

$helper = $fb->getRedirectLoginHelper();

PHP Snippet 2:

session_start();
include_once 'path/to/fb-config.php';

try {
    $accessToken = $helper->getAccessToken();
} catch (\Facebook\Exceptions\FacebookResponseException $e) {
    echo "Response Exception: " . $e->getMessage();
    exit();
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
    echo "SDK Exception: " . $e->getMessage();
    exit();
}

/** THE REST OF YOUR CALLBACK CODE **/

PHP Snippet 3:

http://example.com/fb-callback.php

AND ALSO

http://www.example.com/fb-callback.php

PHP Snippet 4:

$redirectURL = "http://".$_SERVER['SERVER_NAME']."/fb-callback.php";
$permissions = ['email'];
$fLoginURL = $helper->getLoginUrl($redirectURL, $permissions);

PHP Snippet 5:

///FACEBOOK SIGNUP

session_start();
include_once 'config-facebook.php';

try {
$accessToken = $helper->getAccessToken();
 } catch (\Facebook\Exceptions\FacebookResponseException $e) {
  echo "Response Exception: " . $e->getMessage();
  exit();
 } catch (\Facebook\Exceptions\FacebookSDKException $e) {
  echo "SDK Exception: " . $e->getMessage();
  exit();
  }

 $redirectURL = "http://".$_SERVER['SERVER_NAME']."/create-acc.php";
 $permissions = ['email'];
 $fLoginURL = $helper->getLoginUrl($redirectURL, $permissions);


  $facebook_button ='

 <div style="background-color:white; color:#4b5563;    cursor:pointer;" class="inline-flex border-2 py-1.5 px-5 rounded text-lg border-gray-300">
         <div style="margin-top:5px;">
    <img style="width:25px;" src="./assets/apps/facebook-logo-2019.png"/>
 </div>
  <a href="'.$fLoginURL.'"  style="margin:5px 10px;"><b>Sign up with Facebook</b></a>
     </div>


         ';

PHP Snippet 6:

include_once 'Facebook/autoload.php';

$fb = new \Facebook\Facebook([
'app_id' => '**************',
'app_secret' => '*************',
'default_graph_version' => 'v2.10'
  ]);


   $helper = $fb->getRedirectLoginHelper(); 

PHP Snippet 7:

///FACEBOOK SIGNUP

session_start();
include_once 'config-facebook.php';

if (isset($accessToken))
 {
if (!isset($_SESSION['facebook_access_token'])) 
{
    //get short-lived access token
    $_SESSION['facebook_access_token'] = (string) $accessToken;
    
    //OAuth 2.0 client handler
    $oAuth2Client = $fb->getOAuth2Client();
    
    //Exchanges a short-lived access token for a long-lived one
    $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
    $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
    
    //setting default access token to be used in script
    $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
  } 
  else 
  {
    $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}



if (isset($_GET['code'])) 
{
    header('Location: ./');
}


 try {
    $fb_response = $fb->get('/me?fields=name,first_name,last_name,email');
    $fb_response_picture = $fb->get('/me/picture?redirect=false&height=200');
    
    $fb_user = $fb_response->getGraphUser();
    $picture = $fb_response_picture->getGraphUser();
    
    $_SESSION['fb_user_id'] = $fb_user->getProperty('id');
    $_SESSION['fb_user_name'] = $fb_user->getProperty('name');
    $_SESSION['fb_user_email'] = $fb_user->getProperty('email');
    $_SESSION['fb_user_pic'] = $picture['url'];
    
    
} catch(Facebook\Exceptions\FacebookResponseException $e) {
    echo 'Facebook API Error: ' . $e->getMessage();
    session_destroy();
    
    header("Location: ./");
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    echo 'Facebook SDK Error: ' . $e->getMessage();
    exit;
}
 } 
 else 
 {  


 $redirectURL = "http://".$_SERVER['SERVER_NAME']."/create-acc.php";
 $permissions = ['email'];
 $fLoginURL = $helper->getLoginUrl($redirectURL, $permissions);

     $facebook_button ='

 <div style="background-color:white; color:#4b5563;    cursor:pointer;" class="inline-flex border-2 py-1.5 px-5 rounded text-lg border-gray-300">
         <div style="margin-top:5px;">
    <img style="width:25px;" src="./assets/apps/facebook-logo-2019.png"/>
 </div>
  <a href="'.$fLoginURL.'"  style="margin:5px 10px;"><b>Sign up with Facebook</b></a>
     </div>


         ';

}

PHP Snippet 8:

 session_start();
include_once 'Facebook/autoload.php';

 $fb = new \Facebook\Facebook([
'app_id' => '************',
'app_secret' => '************',
'default_graph_version' => 'v2.10'
  ]);


  $helper = $fb->getRedirectLoginHelper(); 

 try {
if(isset($_SESSION['facebook_access_token']))
    {$accessToken = $_SESSION['facebook_access_token'];}
else
    {$accessToken = $helper->getAccessToken();}
} catch(FacebookResponseException $e) {
 echo 'Facebook API Error: ' . $e->getMessage();
  exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK Error: ' . $e->getMessage();
  exit;
 }