Community Documentation

hook_init

5 core.php hook_init()
6 core.php hook_init()
7 system.api.php hook_init()
8 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()

conf_init in includes/bootstrap.inc
Loads the configuration and sets the base URL correctly.
phptemplate_init in themes/engines/phptemplate/phptemplate.engine
tablesort_init in includes/tablesort.inc
Initialize the table sort context.
variable_init in includes/bootstrap.inc
Load the persistent variable table.

File

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

Code

<?php
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']);
    }
  }
}
?>
Login or register to post comments