My goal is to grab the user's previous login value from the user table immediately after logging in. Then set a 'last login' cookie (or some kind of global) to grab through-out the session. Is there any functionality in user.module (or somewhere else?) that can be overridden before user_authenticate_finalize() writes over the login value to the current date?

I've tried using hook_user ( http://api.drupal.org/api/function/hook_user ) for both the 'load' and the 'login' operations, but each show that the user object's login variable has already been written over to the current login date.


function mymodule_user{
  if ($op == 'login') {
    if($account->login && !$_COOKIE['last_login']){
      setcookie('last_login', $account->login);
    }
  }
}

For reference, this is the code in user.module that I somehow need to "get in between".


function user_authenticate($form_values = array()) {
  global $user;

  // Load the account to check if the e-mail is denied by an access rule.
  // Doing this check here saves us a user_load() in user_login_name_validate()
  // and introduces less code change for a security fix.
  $account = user_load(array('name' => $form_values['name'], 'pass' => trim($form_values['pass']), 'status' => 1));
  if ($account && drupal_is_denied('mail', $account->mail)) {
    form_set_error('name', t('The name %name is registered using a reserved e-mail address and therefore could not be logged in.', array('%name' => $account->name)));
  }

  // Name and pass keys are required.
  // The user is about to be logged in, so make sure no error was previously
  // encountered in the validation process.
  if (!form_get_errors() && !empty($form_values['name']) && !empty($form_values['pass']) && $account) {
    $user = $account;
    user_authenticate_finalize($form_values);
    return $user;
  }
}

Comments

work77’s picture

I got it. Still using the hook_user func with the load operation, but added a static var to assure it's only set once. For anybody else interested...

put this in your module...

function mymodule_user{
   if ($op == 'load') {
    static $setLastLog;

    if($account->login && !$_COOKIE['last_login'] && !$setLastLog){
      setcookie('last_login', $account->login);
      $setLastLog = true;
    }
  }
  //and don't forget to clear the cookie on logout
  if ($op == 'logout') {
      setcookie('last_login', '');
  }
}

Then refer to it wherever in your code with something similar to this...

echo format_date($_REQUEST['last_login'], 'custom', 'm/d/Y' );