Facebook PHP SDK: how to get $ helper-> getSessionFromRedirect () to return a valid session

I'm using the PHP PHP SDK to login, but when the user comes back from the Facebook login url, it $helper->getSessionFromRedirect()

returns after login NULL

, not with a valid session.

I followed the Facebook docs , read the answers and comments on these similar questions:

  • php facebook login getSessionFromRedirect is null (I don't have enough dots to post links to everything)
  • Facebook php sdk: getSessionFromRedirect returns null
  • Why does facebook getSessionFromRedirect () method return NULL despite user login?

And I found other solutions online, and my code matches the solutions others say work for them, but I'm missing something.

I have my Facebook app setup and the site url and app domain are set to the url of the app that is included.

I have two pages in place.

The first has a link "Log in with Facebook".

And the second one processes the response from Facebook and should start a user session.

Here's the code for the login page:

session_start();

// using Composer
require 'vendor/autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;

$app_id = 'my_id';
$app_secret = 'my_secret';
$redirect_url = 'my_redirect_url';

FacebookSession::setDefaultApplication($app_id, $app_secret);

$helper = new FacebookRedirectLoginHelper($redirect_url);
$loginUrl = $helper->getLoginUrl();

echo '<a href="' . $loginUrl . '">Login with Facebook</a>';

      

And here's the code for the second page, to get an answer after login app.php

.

session_start();

require 'vendor/autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;

$app_id = 'my_id';
$app_secret = 'my_secret';
$redirect_url = 'my_redirect_url';

FacebookSession::setDefaultApplication($app_id, $app_secret);

$helper = new FacebookRedirectLoginHelper($redirect_url);
$session = $helper->getSessionFromRedirect();

var_dump($helper);

      

This is what I get from var_dump($helper)

, please note at session: NULL

the end:

object(Facebook\FacebookRedirectLoginHelper)#2 (6) { ["appId":"Facebook\FacebookRedirectLoginHelper":private]=> string(16) "my_id" ["appSecret":"Facebook\FacebookRedirectLoginHelper":private]=> string(32) "my_secret" ["redirectUrl":"Facebook\FacebookRedirectLoginHelper":private]=> string(32) "my_redirect_url" ["sessionPrefix":"Facebook\FacebookRedirectLoginHelper":private]=> string(6) "FBRLH_" ["state":protected]=> string(32) "a_32_character_string" ["checkForSessionStatus":protected]=> bool(true) } session: NULL

      

Any help would be awesome. This is my first question, so if there is anything I can do to improve it please let me know.

+3


source to share





All Articles