0

I need to pass parameters from a Drupal page to a PHP script. There's no way to pass that directly and I need to use SESSIONS. I've searched around and added this at the top of the script:

define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);

With that I can use Drupal's session but I'm getting this error:

Warning: session_start(): Cannot send session cache limiter - headers already sent

It's not so bad since it's just a warning and I can use CSS to make it not visible to the user. But I was wondering if there is a cleaner way to do so.

8
  • Can the PHP script not be a Drupal module? Commented Aug 14, 2013 at 10:37
  • Unfortunately no... It's a script for credit card company and i must do what they say...
    – doron
    Commented Aug 14, 2013 at 11:18
  • Have you tried not bootstrapping Drupal and just trying to print_r($_SESSION); to see if what you need is in there? Commented Aug 19, 2013 at 14:15
  • of course...thats the first thing i did.
    – doron
    Commented Aug 19, 2013 at 19:13
  • are you including anything above the code you provided? Maybe another include/require is printing out a header. Commented Aug 19, 2013 at 20:04

2 Answers 2

1

In instances where I've had to do things like this I've done the following. It's worked for D6/D7 without any warnings for me.

  $current_path = getcwd(); //Current app path

  //Remove any HTTP headers/output
  header_remove();
  ob_clean();

  //Bootstrap Drupal w/ dir change 
  //$drupal_root being the root Drupal install path
  define('DRUPAL_ROOT', $drupal_root);
  chdir($drupal_root);
  require_once "$drupal_root/includes/bootstrap.inc";

  /*Execute Code Dependant on Drupal Environment */

  chdir($current_path); //Return to existing app path
0

Verify that you do not print anything to the output buffer (with functions like echo or print) before setting/defining headers (with functions like header)

4
  • Thank you for the answer, but i cannot control this... i'm a regular drupal page(hook_menu) and in this page i've put an iframe with the php script... so basically drupal bootstrap runs twice and i'm guessing this is the problem but i don't know how to solve this
    – doron
    Commented Aug 14, 2013 at 9:09
  • Try to add ob_start(); directly after the 3 lines you posted, this will start another output buffer preventing to send headers
    – SebCorbin
    Commented Aug 14, 2013 at 9:17
  • Nope, this didn't do the trick...
    – doron
    Commented Aug 14, 2013 at 16:36
  • Why are you starting drupal bootstrap in your script if it is already started before ?? You shouldn't need to do this.
    – Lovau
    Commented May 19, 2014 at 13:27

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