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['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, 2012 at 12:22
  • Why don't you use the correct module for Facebook integration? For example like Drupal for Facebook.
    – Sk8erPeter
    Apr 4, 2012 at 0:29

1 Answer 1


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
// 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
// including bootstrap file...

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

// 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

// ...
// ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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