Status message

Maintenant, vous regardez: Eight Phases of Drupal 7 Bootstrap

Eight Phases of Drupal 7 Bootstrap

The moment a web browser requests a page, Drupal begins running a complex series of steps that result
in a fully rendered page being returned to the browser. With every page request, Drupal has to do those
same calculations,

The web server’s PHP interpreter parses index.php and executes the code. Drupal’s developers have
organized the process of creating a Drupal page into two sequences: bootstrap and execution of the page
callback associated with the current path. This division allows the use of a working Drupal environment
for applications other than generating web pages

All start with index.php file:

  1. /**
  2.  * @file
  3.  * The PHP page that serves all page requests on a Drupal installation.
  4.  *
  5.  * The routines here dispatch control to the appropriate handler, which then
  6.  * prints the appropriate page.
  7.  *
  8.  * All Drupal code is released under the GNU General Public License.
  9.  * See COPYRIGHT.txt and LICENSE.txt.
  10.  */
  12. /**
  13.  * Root directory of Drupal installation.
  14.  */
  15. define('DRUPAL_ROOT', getcwd());
  17. require_once DRUPAL_ROOT . '/includes/';
  18. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  19. menu_execute_active_handler();

Now, let's take a look at the files,
The task of the bootstrap is to set the stage for business logic and theming to take place by including all
necessary libraries, preparing a database connection, and reading the configuration. It is accomplished
in separate phases, and each phase must be executed only once and in a particular order. This is
enforced by the drupal_bootstrap() function and by having a constant integer assigned to the phases
that represent their processing order , drupal_bootstrap() is called with the bootstrap
phase that should be reached as a parameter.

  1. /**
  2. * First bootstrap phase: initialize configuration.
  3. */
  6. /**
  7. * Second bootstrap phase: try to serve a cached page.
  8. */
  11. /**
  12. * Third bootstrap phase: initialize database layer.
  13. */
  16. /**
  17. * Fourth bootstrap phase: initialize the variable system.
  18. */
  21. /**
  22. * Fifth bootstrap phase: initialize session handling.
  23. */
  24. define('DRUPAL_BOOTSTRAP_SESSION', 4);
  26. /**
  27. * Sixth bootstrap phase: set up the page header.
  28. */
  31. /**
  32. * Seventh bootstrap phase: find out language of the page.
  33. */
  36. /**
  37. * Final bootstrap phase: Drupal is fully loaded; validate and fix input data.
  38. */
  39. define('DRUPAL_BOOTSTRAP_FULL', 7);
  41. /**
  42. * Ensures Drupal is bootstrapped to the specified phase.
  43. *
  44. * In order to bootstrap Drupal from another PHP script, you can use this code:
  45. * @code
  46. * define('DRUPAL_ROOT', '/path/to/drupal');
  47. * require_once DRUPAL_ROOT . '/includes/';
  48. * drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  49. * @endcode
  50. *
  51. * @param int $phase
  52. * A constant telling which phase to bootstrap to. When you bootstrap to a
  53. * particular phase, all earlier phases are run automatically. Possible
  54. * values:
  55. * - DRUPAL_BOOTSTRAP_CONFIGURATION: Initializes configuration.
  56. * - DRUPAL_BOOTSTRAP_PAGE_CACHE: Tries to serve a cached page.
  57. * - DRUPAL_BOOTSTRAP_DATABASE: Initializes the database layer.
  58. * - DRUPAL_BOOTSTRAP_VARIABLES: Initializes the variable system.
  59. * - DRUPAL_BOOTSTRAP_SESSION: Initializes session handling.
  60. * - DRUPAL_BOOTSTRAP_PAGE_HEADER: Sets up the page header.
  61. * - DRUPAL_BOOTSTRAP_LANGUAGE: Finds out the language of the page.
  62. * - DRUPAL_BOOTSTRAP_FULL: Fully loads Drupal. Validates and fixes input
  63. * data.
  64. * @param boolean $new_phase
  65. * A boolean, set to FALSE if calling drupal_bootstrap from inside a
  66. * function called from drupal_bootstrap (recursion).
  67. *
  68. * @return int
  69. * The most recently completed phase.
  70. */
  71. function drupal_bootstrap($phase = NULL, $new_phase = TRUE) {
  72. // Not drupal_static(), because does not depend on any run-time information.
  73. static $phases = array(
  82. );
  83. // Not drupal_static(), because the only legitimate API to control this is to
  84. // call drupal_bootstrap() with a new phase parameter.
  85. static $final_phase;
  86. // Not drupal_static(), because it's impossible to roll back to an earlier
  87. // bootstrap state.
  88. static $stored_phase = -1;
  90. if (isset($phase)) {
  91. // When not recursing, store the phase name so it's not forgotten while
  92. // recursing but take care of not going backwards.
  93. if ($new_phase && $phase >= $stored_phase) {
  94. $final_phase = $phase;
  95. }
  97. // Call a phase if it has not been called before and is below the requested
  98. // phase.
  99. while ($phases && $phase > $stored_phase && $final_phase > $stored_phase) {
  100. $current_phase = array_shift($phases);
  102. // This function is re-entrant. Only update the completed phase when the
  103. // current call actually resulted in a progress in the bootstrap process.
  104. if ($current_phase > $stored_phase) {
  105. $stored_phase = $current_phase;
  106. }
  108. switch ($current_phase) {
  110. _drupal_bootstrap_configuration();
  111. break;
  114. _drupal_bootstrap_page_cache();
  115. break;
  118. _drupal_bootstrap_database();
  119. break;
  122. _drupal_bootstrap_variables();
  123. break;
  126. require_once DRUPAL_ROOT . '/' . variable_get('session_inc', 'includes/');
  127. drupal_session_initialize();
  128. break;
  131. _drupal_bootstrap_page_header();
  132. break;
  135. drupal_language_initialize();
  136. break;
  139. require_once DRUPAL_ROOT . '/includes/';
  140. _drupal_bootstrap_full();
  141. break;
  142. }
  143. }
  144. }
  145. return $stored_phase;
  146. }

1. First Bootstrap Phase: Initialize Configuration
In phase one, settings.php is read from the sites/default folder and the most important global variables
are set, either directly from settings.php like $databases or by computing their value based on the server
environment. Here are three you will need in your daily site developer’s life:
$base_url: The base URL all your Drupal pages share. Each path is appended to it.
It must be a valid URL without a trailing slash. This only needs to be set if Drupal
does not determine it correctly.
$base_url = '<a href="';">';</a> // NO trailing slash!1
$base_path: The base URL’s path component (either ‘/’ or anything following the
domain part) with a trailing slash appended. It is derived from $base_url and can
be handy on its own.
$base_path = '/drupal/';
$base_root: Contains the protocol and domain parts of the URL. It is either the
base URL or derived from it by removing the base path if there is any.
$base_root = '<a href="';">';[/geshifilter-ruby]

2. Second Bootstrap Phase: Try to Serve a Cached Page
During the second bootstrap phase Drupal tries to deliver the whole page from its cache in case page
caching is enabled in the Performance section of the configuration interface and the visitor is not logged
in. If a cached version of the page can be found and is not expired, it is sent between invoking hook_boot() and hook_exit().
If the cache back end requires a database connection (determined by
$conf['page_cache_without_database'] in settings.php), the third and fourth bootstrap phases are
executed before fetching the cached page.
Debugging page caching is eased by an additional HTTP header the Drupal developers have
introduced for this purpose: if the page is actually served from cache, the X-Drupal-Cache HTTP header
is set to HIT; otherwise, its value is set to MISS

3.Third Bootstrap Phase: Initialize the Database Layer
The database abstraction layer is set up in this phase. Because there’s no need to open a connection yet,
only the base classes and utility functions (db_query, et al) are included. In addition, callbacks for
autoloading classes and interfaces are registered with the Standard PHP Library (SPL) autoload stack3.
The files containing classes and interfaces are declared by modules’ .info files or live in the main include
folder, and Drupal maintains a registry to track them. The first time a class or interface is needed during
execution, the callbacks use this registry to include the necessary file.

4. Fourth Bootstrap Phase: Initialize the Variable System
In the fourth bootstrap phase, Drupal fetches all values from the variable database table (which includes
both configuration settings and persistent variables) and merges them with those defined in
settings.php in the global variable $conf. Values set in the file via $conf['variable_name'] take
precedence over those stored in the database; in other words, you can prevent variables from being
overridden through the UI by defining them in the settings.php file.

The $conf variable takes the form of a giant associative array. Its values can be obtained by calling
variable_get('key_name', 'a default value'). Variables can be persisted by calling
variable_set('key_name', 'value') in your code

In addition to the variables, all modules required during bootstrap are loaded, implementing hooks
called during bootstrap, hook_boot(), hook_exit(), hook_language_init(), and hook_watchdog().

5.Fifth Bootstrap Phase: Initialize Session Handling
Things covered so far: database, settings, variables, some common Drupal functions and constants,
global variables, and bootstrap modules.
In this phase, Drupal registers its session handler and a session is associated for already
authenticated users. Usually, anonymous users won’t get a session at all unless something needs to be
stored in $_SESSION; for this case, a session ID is pregenerated. This allows HTTP proxy caching for
anonymous visitors. If Drupal can’t detect a logged in user, a dummy user object is created during this
phase that represents the anonymous visitor with a user ID 0 in Drupal’s database.
If you need fancier session handling than Drupal’s own database solution, you can include an
alternative by pointing $['conf']['session_inc'] in settings.php to a file containing the functions that
need to be implemented. Sites with many authenticated visitors can benefit from a more efficient
session storage.

6.Sixth Bootstrap Phase: Set up the Page Header
After all that nice setup, the first output is generated to be sent to the site’s visitor: the HTTP headers.
The default headers Drupal sends to the client only affect caching. To be precise: No byte is sent on its
way to the visitor because Drupal operates in output buffering mode. In other words, nothing leaves the
server until the buffer is flushed, which happens at the final stage of the cycle. Wait! Something happens
before: hook_boot() is invoked, giving modules a first opportunity to intervene in the page creation cycle.
Note that this hook must be disabled to support external caching mechanisms

7. Seventh Bootstrap Phase: Find out the Language of the Page
The next-to-last step in the bootstrap process deals with language selection for the current visitor if the
site is multilingual. Once the language is determined, implementations of hook_language_init() can
react, for instance by setting language dependent variables.

Language Negotiation Algorithms
Negotiation algorithms that determine which language to use can be provided by
hook_language_negotiation_info() and existing ones can be modified by
hook_language_negotiation_info_alter(). The providers will have a chance to determine a language in
the order they are set by the site admin at the language configuration page
(admin/config/regional/language/configure). Each defined negotiation algorithm must provide a
callback to determine the language. Language selection stops as soon as a provider returns a valid
Here is an example of a language provider:

  1. $providers[LOCALE_LANGUAGE_NEGOTIATION_URL] = array(
  3. 'callbacks' => array(
  4. 'language' => 'locale_language_from_url',
  5. 'switcher' => 'locale_language_switcher_url',
  6. 'url_rewrite' => 'locale_language_url_rewrite_url',
  7. ),
  8. 'file' => $file,
  9. 'weight' => -8,
  10. 'name' => t('URL'),
  11. 'description' => t('Determine the language from the URL (Path prefix or domain).'),
  12. 'config' => 'admin/config/regional/language/configure/url',
  13. );

8. Final Bootstrap Phase: Load Modules and Initialize Theme
Finally drupal_bootstrap_full() is executed. All files containing the Drupal utility functions are
included. Enabled modules are loaded (in other words, the .module files are included). Modules get a
chance to register stream wrappers4 by means of hook_stream_wrappers() and modify existing ones with
hook_stream_wrapper_alter().The path in $_GET['q'] is normalized and the theme is initialized

Note: You can replace all or some of the original functions of Drupal’s menu system and path handling functions
by providing an alternative or in settings.php. You might want to do this to improve the
performance of your Drupal site.


Comments pharmacy canada plus
online pharmacies tech school
canadian pharmacies shipping to usa

That is very interesting, You are a very skilled blogger.

I have joined your rss feed and sit up for seeking more of your great post.

Also, I have shared your web site in my social networks

Here is my website; magazin pescuit how safe are canadian online pharmacies
canadian online pharmacies rated
canada online pharmacies medication canada online pharmacies
canadian discount pharmacies in canada
online pharmacies of canada top rated canadian pharmacies online
canada medications cheap
canadian pharmacies stendra

After looking over a few of the blog posts on your

web site, I honestly appreciate your technique of blogging.

I added it to my bookmark site list and will be checking back

in the near future. Take a look at my web site as well and let me

know your opinion.

my website v bucks free generator aarp recommended canadian pharmacies
Canadian Pharmacy USA
canadian prescription drugstore safe canadian online pharmacies
good canadian online pharmacies
aarp recommended canadian pharmacies

Greetings from Colorado! I'm bored at work so I decided to check out your blog on my

iphone during lunch break. I really like the info you

provide here and can't wait to take a look when I get home.

I'm surprised at how fast your blog loaded on my

mobile .. I'm not even using WIFI, just 3G .. Anyhow, superb site!

Feel free to visit my blog post ... free vbucks

Hi! Someone in my Myspace group shared this site with us so I came to give it a look.

I'm definitely enjoying the information. I'm book-marking and will be

tweeting this to my followers! Wonderful blog and amazing design.

Here is my webpage v bucks free canadian pharmaceuticals nafta
canada online pharmacies
northwest pharmacies in canada

great publish, very informative. I wonder why the opposite specialists

of this sector do not realize this. You must continue your writing.

I'm sure, you have a great readers' base already!

Check out my web blog ... free vbucks pharmacy canada 24
legitimate canadian mail order pharmacies
drugstore online canada canada medication cost
canadian discount pharmacies in ocala fl
canadian medications 247 canada online pharmacies legitimate
pharmacy onesource
canadian medications 247 top rated canadian pharmacies online
prescriptions from canada without
canadian online pharmacies reviews cialis canadian pharmacy
good canadian online pharmacies
pharmacy onesource canada online pharmacies for men
canadian pharmacies without an rx
canadian mail order pharmacies canadian pharcharmy online24
canadian pharmacies that ship to us
canadianpharmacyusa24h canadian pharmaceuticals
legitimate canadian mail order pharmacies
pharmacy canada best

It's remarkable designed for me to have a site, which is helpful

for my experience. thanks admin

Also visit my blog post: free the vbucks canadian pharmacy no prescription
canadian drug store
canadian pharmacy world legitimate canadian mail order pharmacies
canadian drug store
canada medication

I need to to thank you for this excellent read!! I certainly enjoyed

every little bit of it. I've got you saved as a favorite

to look at new stuff you post…

Here is my blog free vbucks canada online pharmacies reviews
are canadian online pharmacies safe
pharmacy canada reviews viagra canadiense
discount canadian pharmacies
canadian pharmacy viagra

Unquestionably believe that that you said. Your favorite justification seemed to be at the web the easiest thing to bear in mind of.

I say to you, I definitely get annoyed while other folks think about

concerns that they plainly don't recognize about. You managed

to hit the nail upon the top as smartly as outlined out the entire thing with no need

side effect , people could take a signal. Will likely be again to get more.


My blog :: free the vbucks canadian pharmacy no prescription
best canadian pharmacy
canadianpharmacyusa24h canadian mail order pharmacies
pharmacy canada online prescriptions
canadian pharmaceuticals for usa sales

hi!,I really like your writing very much! proportion we keep up a correspondence extra about your post on AOL?

I require a specialist on this area to resolve my problem.

Maybe that is you! Looking forward to look you.

My site: v bucks free canadian pharmacies shipping to usa
trusted pharmacy canada
online canadian discount pharmacies

Nice post. I learn something totally new and challenging on blogs I stumbleupon every day.

It's always interesting to read content from other authors and use

a little something from their web sites.

Here is my web page ... free v bucks online pharmacy canada
canadian medications by mail
canadian pharmacies that ship to us

Greetings from Florida! I'm bored to death at work so I decided

to check out your site on my iphone during lunch break.

I enjoy the knowledge you present here and can't wait to take a look when I get home.

I'm amazed at how quick your blog loaded on my phone ..

I'm not even using WIFI, just 3G .. Anyhow, awesome blog!

Feel free to visit my webpage; free vbucks canadian drug
north west pharmacies canada
aarp recommended canadian pharmacies

I visited various blogs except the audio feature for audio songs existing at this web page is really


Also visit my web site; free the vbucks

Your method of describing everything in this article is in fact pleasant, all can without difficulty understand it, Thanks a lot.

My web-site; free the vbucks


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
2 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.