jason jason - 6 months ago 25
PHP Question

How can I pass value to FacebookRedirectLoginHelper in PHP

I use Facebook PHP SDK and trying to send token-id from one page to another. I use this code - http://www.krizna.com/demo/login-with-facebook-using-php/. I use

$helper = new FacebookRedirectLoginHelper('http://www.krizna.com/fbconfig.php' );


I have tried sending value like this :

$helper = new FacebookRedirectLoginHelper('http://www.krizna.com/fbconfig.php?value='.$value );


but I do not get value in
fbconfig.php
file when I try:

$value = $_GET['value'];


I have also used session for sending values but it does not work. How can I send value to FacebookRedirectLoginHelper (
fbconfig.php
)?

fbconfig.php

<?php

session_start();

$value = $_GET['value_new'];

$fb = new \Facebook\Facebook([
'app_id' => $config->facebook->app_id,
'app_secret' => $config->facebook->app_secret
]);

$helper = $fb->getRedirectLoginHelper();
try {
if ($access_token = $helper->getAccessToken()) {

try {
// Returns a `Facebook\FacebookResponse` object with the requested fields
$response = $fb->get('/me?fields=name,id,email,picture', $access_token);
$user = $response->getGraphUser();
$fbid = $user->getId(); // To Get Facebook ID
$fbfullname = $user->getName(); // To Get Facebook full name
$femail = $graphObject->getEmail();// To Get Facebook email ID
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
//Then do whatever you want with that data

$value = $_SESSION['value'];

header("Location: index.php?value_new=$value");

} catch (\Facebook\Exceptions\FacebookResponseException $e) {
error_log('Graph returned an error: ' . $e->getMessage());
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
error_log('Facebook SDK returned an error: ' . $e->getMessage());
}
}
} catch (\Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
error_log('Graph returned an error: ' . $e->getMessage());
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
error_log('Facebook SDK returned an error: ' . $e->getMessage());
}


index.php

<?php if (isset($_SESSION['FBID'])): ?><!-- After user login -->
<div class="container">
<h1>value <?php $value_new = $_GET['value_new']; echo $value_new; ?></h1>
</div>
<? endif ?>

Answer

You should not be trying to pass the facebook Access Token via the query string / $_GET[] superglobal. What you should be using is what facebook recommends, which is the $_SESSION[] superglobal.

Since you are trying to pass the access token from one page to another, lets call the access token on the first page $access_token. To pass it to another page do the following:

page 1:

<?php 
  session_start(); //the very top of your document
  // ... other code ...
  $_SESSION['access_token'] = $access_token;
  // ... other code ...
?>

page 2:

<?php 
  session_start(); //the very top of your document
  // ... other code ...
  $access_token = $_SESSION['access_token'];
  // ... other code ...
?>

That should work, let me know if it works for you.