Last updated January 30, 2013. Created by arshadcn on January 13, 2010.
Edited by NASACT, forestmars, technicalknockout, VM. Log in to edit this page.
For Drupal 7:
There are many ways to do this either by site-building with core modules or a number of different contributed modules, or by building a custom module implementing the Drupal API. "Your mileage may vary" with each. Be warned, the login block sometimes behaves differently than the login page. Also, make sure you test the password reset path which can be affected.
Using Drupal core site-building
With Actions & Triggers
Similar to the steps below in the 'For Drupal 6' section, you'll first need to define the URL redirect action at admin/config/system/actions.
Then, after making sure the core Trigger module is enabled at /admin/modules (or using drush) assign the URL redirect action you just defined to the ""After a user has logged in" trigger at /admin/structure/trigger/user.
NOTE: This works from the /user page not any page within your site. It will not redirect from any other page.
Using a contrib module
LoginToboggan
Download and install as usual: http://drupal.org/project/logintoboggan
Then go to settings page for the module and configure.
NB. Toboggan only gives you a custom redirect for newly registered users using immediate login. It does however have Rules integration.
Login Destination
Download and install as usual: http://drupal.org/project/login_destination
Then go to settings page for the module and configure.
Login Destination allows you to redirect onthe basis of evaluated php code (requires php filter.)
Login Redirect
A lightweight but relatively new solution which has support for non-internal redirects.
http://drupal.org/project/login_redirect
Using the Rules module
Download and install as usual: http://drupal.org/project/rules
This module is somewhat similar to Drupal core's Trigger module, but it is more advanced and offers more flexibility/configuration. The setup is similar to the Drupal 6 method below, but configuration takes place with Rule's admin pages.
For Drupal 6:
Step 1 : Enable the trigger module if you haven't done so. (To enable the trigger module, go to administer->site building->module. check the trigger module and press save.)
Step 2 : Go the administration panel and click on action. Under Make a new advanced action available, select Redirect to URL... and hit create.
Enter a description and the url to redirect to. You can use internal url like node/123 to redirect the user on login.
Press Save.
Step 3 : Go to the administration panel and click on triggers under site building. Select users in the tab.
Under Trigger: After a user has logged in, select the trigger you have just created and hit assign.
Logout and log back in to test.
Comments
user specific redirects
Is there a way do redirect users based on roles? Like Auth user #1, Auth user #2, etc... to be sent to different pages?
You can try this module ->
You can try this module -> http://drupal.org/project/login_destination
Arshad Chummun
blog | twitter | app.net
Redirect After Login custom solution
This is a very simplen but functional approach. (dirty)
Drupal always redirects to the user page (by default) ,so you can do this.
Customize User Page using Panels and the Page modules by adding a custom php block where you place a drupal_goto().
Then create your custom user page.
You can create a switcher using GET arguments in order to redirect especifically where you want.
Using access rules (within panels module on custom user variation) you can specify wich role or condition will be needed to perform the custom redirect.
Nice approach. Dirty or not:
Nice approach. Dirty or not: I used it and it does what it needs to do. Thanks for sharing.
You've made the impossible possible!
That's overstating it a bit but using your method in a different context has allowed me to do something I spent ages on not coming up with a solution.
Thanks!
Hi, I just tried and it did
Hi,
I just tried and it did not work for me. I went through every step of the description given above and I was not re-directed to a custom URL I wanted to. Is there something wrong with what I've done?
Thanks
T
Visit our Startups group on Drupal
Currently available for freelance if needed
Same here... seems like it
Same here... seems like it should work, but it doesn't.
No joy
I couldn't get it to work either; after logging in, I wanted the user to be redirected to his profile page. For my action, I selected "Redirect to a tokenized URL". Here are two URL's I tried for the user's destination:
[account-url]user/[uid]On my site's home page, the user is presented with a login form. After the form is submitted, the user is presented the same home page for the site instead of being redirected to his personal home page.
Joy, or something kind of like Joy
As it turns out, I have had success redirecting the user to a specified page following the log in action only if the user logs in via the main login page ( user/login ). When you go to this page, the default URL is something like user/login?destination=node. You can apply a Trigger or Rule to the login action and change what the destination is. However this does not work if the person logs in via a login panel or block that's part of some other page, say the home page.
Thanks
Its really good information
Thanks
I can suggest this solution
I can suggest this solution http://www.allexx.info/drupal_login_redirect It is in Russian, but you can look at the code. In two words the problem is in drupal_goto() function. Redirect works when you login from q=user page, but doesn't work when you login form block.
Not hard to believe..!
And they put this obviously untested module in the "Tutorials" section?
That tells a lot about Drupal and the Drupal documentation.
Nothing ever works the first time. You really have to be an expert at PHP and CSS to be able to fix all the bugs. But they never said anything about Russian!!?
Here is an example for you: In the Profile module page here is an extract of the instructions on the module set up:
"■ Using Content Profile Module: If you are using Content Profile you much select Show a secondary tab below the user's edit tab under Profile edit tab in the content profile content type tab per content type."
I guess there will always be another language one has to learn!
No wonder it is so frustrating...
I have been working on my Drupal site for over two month now and I haven't made any progress to speak of. I am always bogged down by bugs and bad documentation. Too much time spent looking for things. This is going to go on forever...
Time for me to wake up and move on to another CMS.
I tried it, but doesnt worked
I tried it, but doesnt worked for me. One the user logged in it doesnt redirect.
"■ Using Content Profile Module: If you are using Content Profile you much select Show a secondary tab below the user's edit tab under Profile edit tab in the content profile content type tab per content type."
Ya I really find drupal bit confusing.
Another way to redirect after
Another way to redirect after login is to use $_REQUEST['destination']. For example, to redirect the user to her profile:
$_REQUEST['destination'] = 'user';I'm using this on a custom login block and it works just fine.
Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind. -- Dr. Seuss
join www.lexxing.com ! where people and languages meet
Hi. Sorry for the trouble,
Hi. Sorry for the trouble, but im really new in this Drupal thing.
Where do I exactly put that code?
Im using a custom login block just like you.
I've tried lots of ways but they only work if the user is at /user/login...
EDIT: I came up with the idea of calling the login module in my page-front since my homepage will be only the login. So, after (even) more research i found this http://data.agaric.com/node/2382 , edited
<?phpprint drupal_get_form('user_register');
?>
to
<?phpprint drupal_get_form('user_login');
?>
and all the options I've tried before worked, since the login isn't in a block anymore.
Maybe this is common knowledge but I decided to share since lots of you have helped me.
Thanks !
Use a module
Redirect from /user/login to previous page:
function mymodule_form_alter(&$form, $form_state, $form_id) {if( (arg(0)=='user') && (arg(1)=='login') && (!arg(2)) && ($form_id=='user_login') ){ // redirect from login page to previous page
if($_SERVER['HTTP_X_FORWARDED_PROTO']){
$serverstarturl = "https://";
}
else{
$serverstarturl = "http://";
}
if( strpos( $_SERVER['HTTP_REFERER'] , $serverstarturl . $_SERVER['SERVER_NAME'] ) === 0){
if($_SERVER['HTTP_REFERER'] != ($serverstarturl.$_SERVER['SERVER_NAME'].'/user/login') ){
$action=str_replace( ($serverstarturl.$_SERVER['SERVER_NAME'].'/') , '', $_SERVER['HTTP_REFERER'] );
$protocols=array('http', 'https', 'ftp', 'ftps', 'sftp', 'ssh');
$action = str_replace($protocols, '', $action); // stop people hacking action to redirect to other websites
if($action==''){
$action='home';
}
$form['#action']='/user/login?destination=' . $action;
}
}
else{
// do nothing http_referrer has been hacked, or user has come to login page from another site...
}
}
}
Senior Drupal Web Developer
Wellcome Trust Sanger Institute
This solution breaks login
This solution breaks login via Services API ( http://drupal.org/project/services )
I used XML-RPC to manage content on my Drupal website, and found that XML-RPC service is broken after this solution was used.
Just a small note for those who plan to use this approach for Drupal with Services module.
Joseph Chereshnovsky
Found the solution for this
found the user redirect solution without any Triggers and Actions
Just implement hook_user() in custom module and assign
<?php$_REQUEST['destination'] = '<front>' ;
?>
Look at the following article for details
http://www.givegoodweb.com/post/121/drupal-redirecting-a-user-after-login
Joseph Chereshnovsky
Use hook_user_login()
This seems to work for me on Drupal 7.12
<?phpfunction hook_user_login(&$edit, $account) {
$edit['redirect'] = 'node/123';
}
?>
Works for both regular login page and login forms loaded through
drupal_get_form('user_login'). The user login block redirects to the page the user logged on to.Laurens Meurs
Rotterdam, the Netherlands
D7 redirect user after login
This should be fairly simple to accomplish by doing the following.
Action doesn't seem to work
Although the action and trigger method by wstopia sounds like the cleanest way it doesn't work for me. (Drupal 7.12 and login toboggan installed) I dont know why...
But there is a easy solution: http://drupal.org/project/login_destination works perfect.
default login-block action prevents trigger
The default action on the login-block form prevents Trigger to work.
No need to hardcode urls in your module.
The easiest fix is to simply remove this action:
<?php/*
* Implements hook_form_alter()
*/
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_login_block') {
unset($form['#action']);
}
}
?>
And the Trigger 'on user login' will work as expected.
Cheers!
Where?
Thanks for your snippet ziccidus, but from where are we supposed to remove this function?
What ziccidus ment is that
What ziccidus meant is that you have to place the function inside your own module.
The example function MODULENAME_form_alter() is an implementation of the hook hook_form_alter(), a function that is called everytime a form is being built. Whenever the form with ID 'user_login_block' is being built, the example function removes the variable '#action'.
If you do not have experience with building your own custom module, see http://drupal.org/developing/modules.
Laurens Meurs
Rotterdam, the Netherlands
Thank you for your reply,
Thank you for your reply, Laurens
My confusion was dependant from my past Php experiences and specifically from my use of Html ids.
So should another newbie like me pass by the steps to follow are:
1) Create a directory in /sites/all/modules called small_unlock_redirection_module
2) in this directory create a file called small_unlock_redirection_module.info
3) inside small_unlock_redirection_module.info write:
name = Small unlock redirection moduledescription = a reminder that devs like to hardcode too.
core = 7.x
4) always in the same directory create a new file called small_unlock_redirection_module.module
5) in this new file write:
<?php/*
* ziddicus's snippet
*/
function small_unlock_redirection_module_form_alter(&$form, &$form_state, $form_id) {
// user_login_block is not a placeholder but the actual Drupal Id of the login form, silly me.
if ($form_id == 'user_login_block') {
unset($form['#action']);
}
}
?>
6) please note that the closing "?>" should not stay in your file, remove it.
7) go to admin->module and enable the module
8) follow wstopa steps above to create a trigger.
Closing comment,
I modified my module to implement hook_form_FORM_ID_alter() instead of hook_form_alter(),
If you have problems identifying the form ID , install devel and
check "Display $page array" in Admin->configuration->devel.
Thanks for your help.