4.6.x core.php hook_exit($destination = NULL)
4.7.x core.php hook_exit($destination = NULL)
5.x core.php hook_exit($destination = NULL)
6.x core.php hook_exit($destination = NULL)
7.x system.api.php hook_exit($destination = NULL)

Perform cleanup tasks.

This hook is run at the end of most regular page requests. It is often used for page logging and specialized cleanup. This hook MUST NOT print anything because by the time it runs the response is already sent to the browser.

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_init() instead. That is 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.

Parameters

$destination: If this hook is invoked as part of a drupal_goto() call, then this argument will be a fully-qualified URL that is the destination of the redirect.

Related topics

2 string references to 'hook_exit'
HookBootExitTestCase::testHookBootExit in modules/simpletest/tests/bootstrap.test
Test calling of hook_boot() and hook_exit().
system_test_exit in modules/simpletest/tests/system_test.module
Implements hook_exit().
6 functions implement hook_exit()

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

drupal_exit in includes/common.inc
Performs end-of-request tasks.
overlay_exit in modules/overlay/overlay.module
Implements hook_exit().
statistics_exit in modules/statistics/statistics.module
Implements hook_exit().
system_test_exit in modules/simpletest/tests/system_test.module
Implements hook_exit().
system_test_lock_exit in modules/simpletest/tests/system_test.module
Try to acquire a specific lock, and then exit.

... See full list

4 invocations of hook_exit()
ajax_footer in includes/ajax.inc
Performs end-of-Ajax-request tasks.
drupal_exit in includes/common.inc
Performs end-of-request tasks.
drupal_page_footer in includes/common.inc
Performs end-of-request tasks.
_drupal_bootstrap_page_cache in includes/bootstrap.inc
Attempts to serve a page from the cache.

File

modules/system/system.api.php, line 728
Hooks provided by Drupal core and the System module.

Code

function hook_exit($destination = NULL) {
  db_update('counter')
    ->expression('hits', 'hits + 1')
    ->condition('type', 1)
    ->execute();
}

Comments

bkosborne’s picture

This hook MUST NOT print anything because by the time it runs the response is already sent to the browser.

Is that true? I see that drupal_page_footer is executed after the rendered page is printed. Within that function hook_exit is invoked and THEN the output buffer is flushed. Seems like a module could print something here, it would just appear after the tag.

bburg’s picture

Contrary to what the description above says, hook_init is probably inappropriate for most cases where code needs to run once per page request. This is often done when adding javascript of css to a page. However, hook_init gets run for every type of request, including ajax request. So why would you want to add your arbitrary javascript to every ajax callback? you probably don't, since this is usually for specific json data. For these, use hook_page_build or hook_page_build_alter these are only run for the main page request. Better yet, when attaching js or css, use the '#attached' property in render arrays!