I have the following code which redirects the user to log into facebook and tries to retrieve the session but the session is NULL:
<?php
session_start();
require 'vendor/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
FacebookSession::setDefaultApplication('Foo', 'Bar');
$helper = new FacebookRedirectLoginHelper('Baz');
$loginUrl = $helper->getLoginUrl();
echo '<a href="' . $loginUrl . '">Log In</a>';
$session = $helper->getSessionFromRedirect();
// This displays [NULL] always
echo '[' . gettype($session) . ']';
?>
I don't understand why the $session is always NULL. Please help.
Ok, so the goal was to get a
FacebookSession
. There needs to be 2 pages: 1 for displaying the Log In link and the other to accept the instance ofFacebookSession
. In page1.php put:In page2.php put:
Thanks Fosco for your help.
There is a bug in the FacebookRedirectLoginHelper.php line 214!
change the $_GET['state'] with $_SESSION['state'] from this way:
to this:
I had the same problem doing all of the above, and I fixed it by
You can do the session loading, login redirect and API call all from the single page if required. This tutorial shows you how it can be done.
Look at those two docs, those will help you resolving :)
https://developers.facebook.com/docs/php/gettingstarted
https://developers.facebook.com/docs/php/howto/profilewithgraphapi
You should be calling getSessionFromRedirect only on the page Facebook redirects the user to after login. Then store that session somewhere, like $_SESSION, and re-use it elsewhere.
The issue is, when you generate the login url again, it changes the 'state' variable used to protect against CSRF.