PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 11, 2022

[FIXED] Facebook Connect PHP session variable won't set

 February 11, 2022     facebook, facebook-php-sdk, php     No comments   

Issue

I am using the Facebook PHP API 4.0 and trying to get the session variable to be set, so I can use the user's Facebook data.

The app, and Facebook approval page have all worked as expected, but the $session variable remains to be unset no matter what I try. I have tried to find the solution from similar questions on Stack with no success. This is the code I am using:

session_start();


//start fb login stuff
require_once( $config['basedir'].'/Facebook/FacebookSession.php' );
require_once( $config['basedir'].'/Facebook/FacebookRedirectLoginHelper.php' );
require_once( $config['basedir'].'/Facebook/FacebookRequest.php' );
require_once( $config['basedir'].'/Facebook/FacebookResponse.php' );
require_once( $config['basedir'].'/Facebook/FacebookSDKException.php' );
require_once( $config['basedir'].'/Facebook/FacebookRequestException.php' );
require_once( $config['basedir'].'/Facebook/FacebookAuthorizationException.php' );
require_once( $config['basedir'].'/Facebook/GraphObject.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;

// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication($fb_app, $fb_secret);

$helper = new FacebookRedirectLoginHelper( $config['baseurl'] );

try {
  $session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
  // When Facebook returns an error
  echo $ex;
} catch(\Exception $ex) {
  // When validation fails or other local issues
  echo $ex;
}
if ($session) {
  echo 'Session var is finally being seen';
}
//end Facebook login stuff

Solution

The $session remains unset because you aren't logging the user in. You should modify the last part of the code to something like:

if ( isset( $session ) ) {
  echo 'Session var is finally being seen';
} else {
  // show login url
  echo '<a href="' . $helper->getLoginUrl( array() ) . '">Login</a>';
}

If the $session is not set, your code will ask the user to login. After they login, the $helper->getSessionFromRedirect(); part of your code should execute and set $session. See my tutorial on how to tackle this.


Edit:

If you are logging the user in from a different page, you need to make sure that $helper = new FacebookRedirectLoginHelper( '{your-url}' ); is the same on both pages. If they differ, then you will get a exception from the API related to redirect_uri mismatch.



Answered By - Niraj Shah
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing