Drupal - In hook_user_login is there a way to know if this is the first time the user has logged in?

Check for $account's access property. It is a timestemp the user logged in previously.

It should be other than a valid time stamp in case to be a first time login.

function MODULE_user_login(&$edit, $account){
debug($account->access);
}

I made a test on my test site, using the following code.

function code_testing_user_login(&$edit, $account) {
  watchdog('code_testing', 'User name: %name, user access: %access, user login: %login', array('%name' => $account->name, '%access' => $account->access, '%login' => $account->login), WATCHDOG_DEBUG);
}

I then created a test user, and logged in with that account. The code I wrote output the following message:

User name: test, user access: 0, user login: 1340038458

$account->login cannot be used because the user already logged in, when hook_user_login() is invoked. The only way to see if the user already logged in is to check $account->access.

function mymodule_user_login(&$edit, $account) {
  if ($account->access == 0) {
    // The user has never logged in before.
  }
}

Tags:

Users

7