0

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.

Ashvic A Godinho
  • 139
  • 2
  • 10
  • possible duplicate of [How to redirect user to a specific page after they login if they belong to a certain role?](http://stackoverflow.com/questions/3429767/how-to-redirect-user-to-a-specific-page-after-they-login-if-they-belong-to-a-cer) – Felix Eve Jan 23 '14 at 14:41
  • Or this one: http://stackoverflow.com/questions/3003983/drupal-user-login-redirect – Felix Eve Jan 23 '14 at 14:42
  • Or this: http://stackoverflow.com/questions/1013697/after-login-redirect-to-entrance-url – Felix Eve Jan 23 '14 at 14:43
  • Have another: http://stackoverflow.com/questions/1662178/drupal-after-each-user-login-how-can-i-redirect-to-a-user-specific-page – Felix Eve Jan 23 '14 at 14:44

2 Answers2

0

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

drupal_goto($_SERVER["HTTP_REFERER"]);
voodoo417
  • 11,861
  • 3
  • 36
  • 40
-1

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

Shahin Khani
  • 199
  • 4
  • 14
  • 1
    if ($user->uid) WILL filter out logged out users. $user->uid will be 0 for anonymous users. In PHP 0 equates to a boolean false so that code is fine. – Felix Eve Mar 18 '14 at 14:34