How to redirect a user after login to a specific page

⚠️ Warning
This post is more than 10 years old. I do not delete posts, because even old information is still useful, but please know that some material on this page may be outdated or incorrect. Thanks!
Sep 30, 2013

There are some simple Drupal modules that help with login redirection (especially Login Destination), but I often need more advanced conditions applied to redirects, so I like being able to do the redirect inside a custom module. You can also do something similar with Rules, but if the site you're working on doesn't have Rules enabled, all you need to do is:

  1. Implement hook_user_login().
  2. Override $_GET['destination'].

The following example shows how to redirect a user logging in from the 'example' page to the home page (Drupal uses to signify the home page):

<?php
/**
 * Implements hook_user_login().
 */
function mymodule_user_login(&$edit, $account) {
  $current_path = drupal_get_path_alias($_GET['q']);

  // If the user is logging in from the 'example' page, redirect to front.
  if ($current_path == 'example') {
    $_GET['destination'] = '<front>';
  }
}
?>

Editing $edit['redirect'] or using drupal_goto() inside hook_user_login() doesn't seem to do anything, and setting a Location header using PHP is not best practice. Drupal uses the destination parameter to do custom redirects, so setting it anywhere during the login process will work correctly with Drupal's built in redirection mechanisms.