Is there any way to automatic login user after a successful registration, it seems I can't find any tuts/solution how to do this.Thanks!!

Any replies are much appreciated....

in my template.php

function drupal_onrevue1_form_user_register_submit($form, &$form_state){

    //$form_state['rebuild'] = TRUE;
    $form_state['redirect'] = "user/login";
   $edit = array(
          'name' => $form_state['values']['name'], 
          'pass' => $form_state['values']['current_pass'],
          'field_first_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_first_name']))),
          'field_last_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_last_name']))),
          'field_middle_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_middle_name']))),
          'field_year_select' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_year_select']))),
          'field_month_select' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_month_select']))),
          'field_date_select' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_date_select']))),
          'field_gender' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_gender_select']))),
          'mail' => $form_state['values']['field_mail'],
          'init' => $form_state['values']['field_mail'], 
          'status' => 1, 
          'access' => REQUEST_TIME,

    $account->is_new = TRUE;
    $hello = user_save($account, $edit);

  • user_authenticate() API allows to log in a user. You can use it or use some modules
    – GoodSp33d
    Aug 1, 2012 at 11:50
  • 1
    Thanks...using user_authenticate() I solved the issue and adding by this code $form_state['uid'] = $userRecord; user_login_submit(array(), $form_state); Aug 2, 2012 at 2:11
  • @JunnelGallemaso Is drupal_onrevue1_form_user_register_submit a hook? It's best to write this kind of function in a custom module rather than the theme's template.php file because the theme should only deal with front end output (i.e. the rendering, styling pages) not contain any business logic. Jan 25, 2018 at 15:30
  • drupal_onrevue1_form_user_register_submit is a submit handler I would guess. Pleased it worked for you - thank you, you might also like to add how you added your submit handler into the form (I know how to do it myself), but other users might benefit. See also my solution. Feb 1, 2018 at 11:50

5 Answers 5


Have a look at the Logintoboggan module. This offers you to login new users immediately.


After saving the users' data..I used to user_authenticate() function for authenticating the username and password. Since the return value of it is the uid of the user, you can now used the user_login_submit() function

$username = $form_state['values']['name'];
$pass = trim($form_state['values']['current_pass']);
$userRecord = user_authenticate($username, $pass);
$form_state['uid'] = $userRecord; 
user_login_submit(array(), $form_state); 

Normally drupal will automatically login after successful registration , may be you have wrong settings under 'user > settings'


Ha.. This one is simple. You just have to assign $GLOBALS['user'] variable to the newly created user object.

$fully_loaded_user_object = user_save($account); //returns user obj on success otherwise FALSE
if ($fully_loaded_user_object) {
  $GLOBALS['user'] = $fully_loaded_user_object;

I found this to work in Drupal 7.56 (and I should think earlier and later Drupal 7 versions too).

After a user is created, the hook_user_insert is fired. So the solution is to implement this hook in a custom module as follows:

 * implements hook_user_insert(&$edit, $account, $category)
function mymodule_user_insert(&$edit, $account, $category) {
    $newly_registered_user_id = $account->uid;

    // credit: ML and https://www.drupal.org/project/login_activity/issues/2066899 (second snippet of code)
    global $user;
    $user = user_load($newly_registered_user_id);

Credit to my colleague xiwar for finding the second snippet of code at Programmatically login user in drupal 7 Note that even though this issue was raised as an issue on Login Activity contrib module, this module is not required as the $user variable, user_load and drupal_session_regenerate used in the hook are part of core as indeed the base hook, hook_user_insert is as well. In fact my code base doesn't have Login Activity module.

I did also try calling this code from my custom submit handler for the user registration form but although my debugging proved that the custom submit handler was called on submit, the code for auto-login did not work there. So that's why I implemented my hook for hook_user_insert in my custom module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.