Andres SK
Andres SK

Reputation: 10974

Why is $helper->getSessionFromRedirect() always returning NULL?

I had this code and it was working fine for months. Suddenly, the production environment stopped working, where $helper->getSessionFromRedirect() is now always returning a NULL value.

login form page (index.php)

require 'vendor/autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;

FacebookSession::setDefaultApplication(123456, 'a1b2c3d4e5');

$helper = new FacebookRedirectLoginHelper('http://domain.com/login.php');
$scope = array('email');
$loginUrl = $helper->getLoginUrl($scope);

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

login processing (login.php)

require 'vendor/autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;

FacebookSession::setDefaultApplication(123456,'a1b2c3d4e5');

$helper = new FacebookRedirectLoginHelper('http://domain.com/login.php');

try {
    # success
    $session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
    # when Facebook returns an error
} catch(\Exception $ex) {
    # when validation fails or other local issues
}

if($session) {
    # do something with user data
    $me = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());

    # redirect to user profile page
    header('Location: http://domain.com/profile.php');
    exit();
} else {
    # $session is always NULL with this code :(
}

This is working on my test App, but it's not working with my production App ID. The only difference between those are, of course, the App ID and Secret ID values. I already made sure I was following the Facebook instructions. Also, I'm using Facebook PHP SDK 4's last version.

Upvotes: 0

Views: 1717

Answers (1)

Andres SK
Andres SK

Reputation: 10974

After a long diagnosis, I found out that there was a discrepancy. Since I was using Cloudflare's SSL feature, the original $helper = new FacebookRedirectLoginHelper('http://domain.com/login.php'); request was made with http:80, but the 2nd $helper = new FacebookRedirectLoginHelper('http://domain.com/login.php'); was made with https:443 ... this returns a no match kind of signature to Facebook, which causes the NULL value in the $session variable.

TL;DR do not mix http and https when making requests to fb.

Upvotes: 1

Related Questions