简体   繁体   中英

Redirecting after Drupal 7 login

I'm trying to get the user to return to the page they were on after logging in. Right now, it takes them to the current page. I'm logging in though my universities LDAP so drupal modules don't work. Here is my user.module code that takes users to the lpad login instead of the drupal login block:

function user_login($form, &$form_state) {
  global $user;

  // If we are already logged on, go to the user page instead.
  if ($user->uid) {
    drupal_goto('user/' . $user->uid);
  }
  header("Location: https://www.cvrc.virginia.edu/login/pc"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
  // Display login form:
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('Username'),
    '#size' => 60,
    '#maxlength' => USERNAME_MAX_LENGTH,
    '#required' => TRUE,
  );

  $form['name']['#description'] = t('Enter your @s username.', array('@s' => variable_get('site_name', 'Drupal')));
  $form['pass'] = array('#type' => 'password',
    '#title' => t('Password'),
    '#description' => t('Enter the password that accompanies your username.'),
    '#required' => TRUE,
  );
  $form['#validate'] = user_login_default_validators();
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Log in'));

  return $form;
}

Any thoughts? Thanks.

(If i understood). Thinking,you need use $_SERVER["HTTP_REFERER"] for returning user back. Try it:

drupal_goto($_SERVER["HTTP_REFERER"]);

first instead of

    if ($user->uid) {
    drupal_goto('user/' . $user->uid);
  }

user either user_is_logged_in() or $user->uid >0 . your current logic will also work for anonymous users which is uid 0

also from the looks of it the code for your form will never get executed.

if you are trying to redirect the user after login i believe you should do this

// If we are already logged on, go to the user page instead.
  if (user_is_logged_in()) {
    drupal_goto('user/' . $user->uid);
  }else{
      // Display login form:
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('Username'),
    '#size' => 60,
    '#maxlength' => USERNAME_MAX_LENGTH,
    '#required' => TRUE,
  );

  $form['name']['#description'] = t('Enter your @s username.', array('@s' => variable_get('site_name', 'Drupal')));
  $form['pass'] = array('#type' => 'password',
    '#title' => t('Password'),
    '#description' => t('Enter the password that accompanies your username.'),
    '#required' => TRUE,
  );
  $form['#validate'] = user_login_default_validators();
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Log in'));

  return $form;
  }
 {pseudocode on success form redirect }
  header("Location: https://www.cvrc.virginia.edu/login/pc"); /* Redirect browser */
  /* Make sure that code below does not get executed when we redirect. */
  exit;

something like that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM