4

I'm building a Facebook iframe app in Drupal.

I'm using an initial simple PHP script (non-Drupal driven) to handle the Facebook open auth side of things - this script is getting the Facebook user's name, id, profile pic etc. This script stores those values to session vars. Then it redirects the user to a page served by Drupal.

My understanding looking at the D7 session.inc is that pre-existing session vars should be maintained when Drupal session start is called. But if I print_r() the $_SESSION array, it is empty.

So essentially a plain PHP script saying:

session_start();
$_SESSION['name'] = 'bob';

sets the session correctly. But when then adding a redirect (header()) to a Drupal served page I can no longer get that name variable and the session is empty.

Any suggestions/tips?

Thanks, Steve

  • 1
    I believe you need to bootstrap Drupal in your custom PHP file using code such as require_once 'path/to/drupal/includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);. That should give you the correct session – Clive Mar 29 '12 at 12:22
  • Why don't you use the correct module for Facebook integration? For example like Drupal for Facebook. – Sk8erPeter Apr 4 '12 at 0:29
4

A really good source related to your question from drupal.org's forum:
Struggling with Session Variables (the $_SESSION array) when redirecting to external php scripts

The key for codes outside Drupal's full control to work is to save PHP's current working directory, change it to Drupal's, requiring the needed file to bootstrap Drupal (includes/bootstrap.inc), pass the appropriate $cookie_domain variable ("the domain to be used for session cookies"!), bootstrapping Drupal, and then change the current working directory back to the original. After this, you can write your own codes and use Drupal's functions, mess with its session variables, etc. - of course, depending on the bootstrapping mode of Drupal.

The code to achieve it looks like this (you have to change $your_drupal_directory variable to yours, usually this is in the root, but its path can be different [e.g. if it's in a subdirectory]):

// ...

/**
 * @see http://drupal.org/node/575002
 */

// store current working directory
$currdir=getcwd();
// which is your Drupal directory? (usually it's in the root, but can be different)
$your_drupal_directory = $_SERVER['DOCUMENT_ROOT'];
// change PHP's current directory to Drupal's
chdir($your_drupal_directory);
// including bootstrap file...
require_once("./includes/bootstrap.inc");

// The domain to be used for session cookies. (see: http://api.drupal.org/api/drupal/developer!globals.php/global/cookie_domain/7)
$cookie_domain = $_SERVER['HTTP_HOST'];

// DRUPAL_BOOTSTRAP_SESSION: initialize session handling.
drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);

// the code above should be enough for your task, but if you want Drupal to be "fully loaded, validate and fix input data", you could try the following too (see: http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_bootstrap/7)
// drupal_bootstrap( DRUPAL_BOOTSTRAP_FULL );

// change PHP's current directory BACK to the original
chdir($currdir);

// ...
// YOUR OWN CODE COMES HERE
// ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.