How can I pass FacebookRedirectLoginHelper value to PHP

I am using the PHP PHP SDK and trying to send an identifier token 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 tried to send the value as follows:

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

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

 $value = $_GET['value']; 

I also used a session to send values, but it does not work. How can I post a 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 ?> 
+5
source share
2 answers

You should not try to pass the Facebook access token through the request string / $_GET[] superglobal. What you should use is what facebook recommends, which is $_SESSION[] superglobal.

Since you are trying to transfer an access token from one page to another, call the access token on the first page of $access_token . To transfer it to another page, follow these steps:

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 ... ?> 

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

+2
source

This answer uses the PHP PHP SDK v5.1.2 and is adapted to the method that I use in my own projects. He also assumes that the composer’s autoload is 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 specify on your redirect page)

 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()); } 

Hope this gives you a hand. I have no doubt that my code has been adapted from previous answers or StackOverflow tutorials.

+1
source

All Articles