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 ?>
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.
This answer is using the Facebook PHP SDK v5.1.2, and is adapted from the method I have working in my own projects. It also assumes composer autoloading is being used.
Login Page
session_start();
$fb = new \Facebook\Facebook([
'app_id' => $config->facebook->app_id, //Change these to yours
'app_secret' => $config->facebook->app_secret
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email'];
$this->view->facebook_url = $helper->getLoginUrl('http://www.krizna.com/fbconfig.php', $permissions);
fbconfig.php
(or whatever you want your redirect page to be)
session_start();
$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
} 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());
}
Hopefully that gives you a hand up. I don't doubt that my code has been adapted from previous StackOverflow answers or tutorials.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With