7 bootstrap.inc drupal_bootstrap($phase = NULL, $new_phase = TRUE)
4.7 bootstrap.inc drupal_bootstrap($phase)
5 bootstrap.inc drupal_bootstrap($phase)
6 bootstrap.inc drupal_bootstrap($phase)
8 bootstrap.inc drupal_bootstrap($phase = NULL)

A string describing a phase of Drupal to load. Each phase adds to the previous one, so invoking a later phase automatically runs the earlier phases too. The most important usage is that if you want to access the Drupal database from a script without loading anything else, you can include bootstrap.inc, and call drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE).

Parameters

$phase: A constant. Allowed values are: DRUPAL_BOOTSTRAP_CONFIGURATION: initialize configuration. DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE: try to call a non-database cache fetch routine. DRUPAL_BOOTSTRAP_DATABASE: initialize database layer. DRUPAL_BOOTSTRAP_ACCESS: identify and reject banned hosts. DRUPAL_BOOTSTRAP_SESSION: initialize session handling. DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE: load bootstrap.inc and module.inc, start the variable system and try to serve a page from the cache. DRUPAL_BOOTSTRAP_LANGUAGE: identify the language used on the page. DRUPAL_BOOTSTRAP_PATH: set $_GET['q'] to Drupal path of request. DRUPAL_BOOTSTRAP_FULL: Drupal is fully loaded, validate and fix input data.

9 calls to drupal_bootstrap()
cron.php in ./cron.php
Handles incoming requests to fire off regularly-scheduled tasks (cron jobs).
drupal_install_system in includes/install.inc
Callback to install the system module.
index.php in ./index.php
The PHP page that serves all page requests on a Drupal installation.
init_theme in includes/theme.inc
Initialize the theme system by loading the theme.
install_main in ./install.php
The Drupal installation happens in a series of steps. We begin by verifying that the current environment meets our minimum requirements. We then go on to verify that settings.php is properly configured. From there we connect to the configured database…

... See full list

File

includes/bootstrap.inc, line 1124
Functions that need to be loaded on every Drupal request.

Code

function drupal_bootstrap($phase) {
  static $phases = array(
    DRUPAL_BOOTSTRAP_CONFIGURATION,
    DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE,
    DRUPAL_BOOTSTRAP_DATABASE,
    DRUPAL_BOOTSTRAP_ACCESS,
    DRUPAL_BOOTSTRAP_SESSION,
    DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE,
    DRUPAL_BOOTSTRAP_LANGUAGE,
    DRUPAL_BOOTSTRAP_PATH,
    DRUPAL_BOOTSTRAP_FULL,
  ), $phase_index = 0;

  while ($phase >= $phase_index && isset($phases[$phase_index])) {
    $current_phase = $phases[$phase_index];
    unset($phases[$phase_index++]);
    _drupal_bootstrap($current_phase);
  }
}

Comments

I wanted to create an API that can be called to create and edit nodes and this API would be called very frequently. My problem was that it was creating a high server load since Drupal was bootstrapped every time. Here are some stress test results I ran (times in seconds based on 100 page loads):

DRUPAL_BOOTSTRAP_DATABASE 0.014402948244654
DRUPAL_BOOTSTRAP_FULL 0.23315950355144
my custom code (see below) 0.04217075819921

As you can see, database access only is the fastest - but I wanted to use node functions and they would not be available in that mode. The full bootstrap gives me that, but it's too slow. So I created my own code:

<?php
chdir
('../../../../..'); //the Drupal root, relative to the directory of the path
require_once './includes/bootstrap.inc';
require_once
'./includes/common.inc';
require_once
'./includes/module.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
drupal_load('module', 'i18n'); // I had to load i18n, otherwise I got some errors. If you don't use it, remove this
module_invoke('i18n', 'boot');
drupal_load('module', 'node');
module_invoke('node', 'boot');

global
$user;
$user->uid = 1;
print_r($user);
$node = node_load(123);
print_r($node);
?>

This let's my super user create and edit nodes, with a load time of less than 1/5 of DRUPAL_BOOTSTRAP_FULL

Because other modules should be able to inject data into $node via hook_nodeapi(). This code will not allow them to do that. You shouldn't avoid a full bootstrap in that case.

I've found that drupal_bootstrap() clobbers my session data (I'm hoping to grab $user-uid) unless either:

A) The php file it's called from is in the root directory
B) $base_path is set in settings.php

I just had to remove my $base_path setting to allow the Domain Access module to work.

Any ideas on how to do a drupal bootstrap with a file that isn't in the root directory? Why is it breaking in the first place?

Just figured this out. Bootstrap expects to be in the root directory when run. I found clues to the solution here. Basically global $base_url needs to be set to the root of the drupal install before bootstrap can occur.

I've used this code to get around that....

chdir($_SERVER['DOCUMENT_ROOT']);
global $base_url;
$base_url = 'http://'.$_SERVER['HTTP_HOST'];
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
global $user;
print $user->uid;

A little hacky, but it gets the job done.

root folder: 11.php

<?php
 
//set the working directory to your Drupal root
chdir('./');
//require the bootstrap include
require_once './includes/bootstrap.inc';
//Load Drupal
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
echo
"print something to have a look";

aaa();

global
$user;
echo
"I am: ".$user->name;
echo
"</br>";
 
$result = db_query("SELECT  subbr, subru, subin, subcn, address1 FROM {subscribe} WHERE user_name = '%s'", $user->name);
       
$re = db_fetch_array($result);
       
print_r ($re);
?>

need to explain, aaa() is a function I written in bootstrap.inc. It is for check whether bootstrap.inc has been require_once.
no problem, for this: the access result is :
___________________________________________________________________
print something to have a look

33333333333333333333

I am: visitor
Array ( [subbr] => 1 [subru] => 1 [subin] => 1 [subcn] => 1 [address1] => Commonwealth House )

________________________________________________________
The problem is here:
when I put it on sub folder eg: root/xxx
Code is the same but change one line: chdir('../');

code here:

<?php
 
//set the working directory to your Drupal root
chdir('../');
//require the bootstrap include
require_once './includes/bootstrap.inc';
//Load Drupal
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
echo
"print something to have a look";

aaa();

global
$user;
echo
"I am: ".$user->name;
echo
"</br>";
 
$result = db_query("SELECT  subbr, subru, subin, subcn, address1 FROM {subscribe} WHERE user_name = '%s'", $user->name);
       
$re = db_fetch_array($result);
       
print_r ($re);
?>

______________________________________________________________
the access result is :
print something to have a look

33333333333333333333

I am:
_________________________________________________________________

the bootstrap.inc has been include but why I just can get database and user info
only in root. any sub folder drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); can not give me the result what I want.