Drupal 6: controlling the user login destination
I was hanging out in #drupal-support this afternoon when an interesting question was asked: how do I redirect users to something other than /user when they log in? Having no prior experience with this I decided to dive in and find out for myself.
There's a module for that
If you're looking for a canned solution, check out Login Destination. This module supports redirecting users to a static url or you can create your own custom php snippet for dynamic redirects.
For those that like to do things the hard way
If you're looking to code this behavior into a custom module it's quite easy to set up. Here's an example that redirects the user to an OG group page once they've logged in:
/**
* Implementation of hook_form_alter()
*
* adds #submit function to redirect user after they've authenticated
*/
function mymodule_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'user_login' || $form_id == 'user_login_block') {
$form['#submit'][] = 'mymodule_redirect';
}
}
/**
* Redirect user to their OG group page
*
* Note: this is noddy as hell as it assumes a user is only a
* member of a single group
*/
function mymodule_login_redirect() {
global $user;
$results = db_query('select nid from {og_uid} where uid=%d', $user->uid);
$nid = db_result($results);
$_REQUEST['destination'] = 'node/' . $nid;
}
As you can see from the above example, setting $_REQUEST['destination'] makes the magic happen.
- freeman's blog
- Login or register to post comments
