Same name and namespace in other branches
  1. 4.6.x developer/hooks/core.php \hook_init()
  2. 5.x developer/hooks/core.php \hook_init()
  3. 6.x developer/hooks/core.php \hook_init()
  4. 7.x modules/system/system.api.php \hook_init()

Perform setup tasks.

This hook is run at the beginning of the page request. It is typically used to set up global parameters which are needed later in the request.

Only use this hook if your code must run even for cached page views. If you have code which must run once on all non cached pages, use hook_menu(!$may_cache) instead. Thats the usual case. If you implement this hook and see an error like 'Call to undefined function', it is likely that you are depending on the presence of a module which has not been loaded yet. It is not loaded because Drupal is still in bootstrap mode. The usual fix is to move your code to hook_menu(!$may_cache).

Return value

None.

Related topics

4 functions implement hook_init()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

conf_init in includes/bootstrap.inc
Loads the configuration and sets the base URL correctly.
phptemplate_init in themes/engines/phptemplate/phptemplate.engine
@file Handles integration of templates written in pure php with the Drupal theme system.
tablesort_init in includes/tablesort.inc
Initialize the table sort context.
variable_init in includes/bootstrap.inc
Load the persistent variable table.
2 invocations of hook_init()
drupal_page_header in includes/bootstrap.inc
Set HTTP headers in preparation for a page response.
_drupal_bootstrap_full in includes/common.inc

File

developer/hooks/core.php, line 502
These are the hooks that are invoked by the Drupal core.

Code

function hook_init() {
  global $recent_activity;
  if (variable_get('statistics_enable_auto_throttle', 0) && !rand(0, variable_get('statistics_probability_limiter', 9))) {
    $throttle = throttle_status();

    // if we're at throttle level 5, we don't do anything
    if ($throttle < 5) {
      $multiplier = variable_get('statistics_throttle_multiplier', 60);

      // count all hits in past sixty seconds
      $result = db_query('SELECT COUNT(timestamp) AS hits FROM
        {accesslog} WHERE timestamp >= %d', time() - 60);
      $recent_activity = db_fetch_array($result);
      throttle_update($recent_activity['hits']);
    }
  }
}