using drupal sessions during hook_boot When a user calls the front page of our site we want to check the country from which the user is surfing and redirect the user directly to the country specific front page.

We chose to use the hook_boot() callback to implement this.

Now we have the problem that we want to save the state "is_redirected" into a global variable that will still be available when the redirected page is loading and is calling our hook_boot() again in order to prevent redirect loops.

We thought about using session management, so we loaded the drupal session code with

include_once(DRUPAL_ROOT . '/includes/session.inc');

The question is now, what is the next step after loading session.inc, do we have to call "drupal_session_initialize()", add a custom entry into the superglobal $_SESSION variable and this custom entry + our session will be available after the redirect?

Thank you in advance!
hook_boot() is invoked too early to use the session, as Drupal has not yet initialised the session. Furthermore, hook_boot() has been removed from Drupal 8. hook_init() is not invoked for cached pages, but there is a trick to avoid the page is cached: Call drupal_page_is_cacheable(FALSE) in hook_init().

The function that actually stores the current page in cache is drupal_page_set_cache(), which contains the following code.

  if (drupal_page_is_cacheable()) {
    $cache = (object) array(
      'cid' =&gt; $base_root . request_uri(), 
      'data' =&gt; array(
        'path' =&gt; $_GET['q'], 
        'body' =&gt; ob_get_clean(), 
        'title' =&gt; drupal_get_title(), 
        'headers' =&gt; array(),
      ), 
      'expire' =&gt; CACHE_TEMPORARY, 
      'created' =&gt; REQUEST_TIME,
    );

    // Restore preferred header names based on the lower-case names returned
    // by drupal_get_http_header().
    $header_names = _drupal_set_preferred_header_name();
    foreach (drupal_get_http_header() as $name_lower =&gt; $value) {
      $cache-&gt;data['headers'][$header_names[$name_lower]] = $value;
      if ($name_lower == 'expires') {
        // Use the actual timestamp from an Expires header if available.
        $cache-&gt;expire = strtotime($value);
      }
    }

    if ($cache-&gt;data['body']) {
      if (variable_get('page_compression', TRUE) &amp;&amp; extension_loaded('zlib')) {
        $cache-&gt;data['body'] = gzencode($cache-&gt;data['body'], 9, FORCE_GZIP);
      }
      cache_set($cache-&gt;cid, $cache-&gt;data, 'cache_page', $cache-&gt;expire);
    }
    return $cache;
  }


When drupal_page_is_cacheable() returns FALSE, the page is not cached; drupal_page_is_cacheable(), when called without arguments, returns the value of the parameter passed to the function in a previous call.

As hook_init() is invoked before drupal_page_set_cache(), calling drupal_page_is_cacheable(FALSE) causes the current page not to be cached.

I would call drupal_page_is_cacheable(FALSE) strictly when necessary, which in you case means when you are sure you need to set $_SESSION['is_redirected'].

Notice that a page is served from the cache when there isn't any session enabled. The function called to get the page from cache (_drupal_bootstrap_page_cache()) contains the following code.

  // If there is no session cookie and cache is enabled (or forced), try
  // to serve a cached page.
  if (!isset($_COOKIE[session_name()]) &amp;&amp; $cache_enabled) {
    // …
  }


If then you need to clear the cache for the front page, you can use the following code.

cache_clear_all(url('&lt;front&gt;', array('absolute' =&gt; TRUE)),  'cache_page');


The other alternative to hook_boot(), and hook_init() I can think of is hook_exit() which is called also for cached pages. The only reason I would not use it is that it is invoked after the current page is rendered, which means that when you redirect the user to a different page, the user would wait for two different pages to be rendered (the one rendered before the redirect, and the one to which the user is redirected).

If you are going to use hook_exit(), you need at least to call drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION); if then you need specific functions, you need to change bootstrap phase, such as with drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL) when you need a function defined from another module. 

Remember that with hook_boot() you cannot call functions defined from other modules, as they are not yet loaded; you can only use PHP functions, and a very strict number of Drupal functions, which at least includes the functions defined in bootstrap.inc.