views:

295

answers:

1

Hello,

When a user enters his login information and hits submit, i want to check if the user already exists or not. So, i have the following two questions 1. Which hook is needed to be implemented , for the case when user hits the submit button on the login form. I need the username entered by the user. 2. How to check if a user already exists in drupal or not programmatically ?

Some sample code would be really appreciated. Please help.

Thank You.

+3  A: 

This can be done with hook_form_alter:

function module_(&$form, &$form_state, $form_id) {
  $user_login_forms = array('user_login', 'user_login_block');
  if (in_array($form_id, $user_login_forms)) {
    $form['#validate'][] = 'my_validate_function';
  }
}

function my_validate_function(&$form, &$form_state) {
  $name = $form_state['values']['name'];
  if (!db_result(db_query("SELECT COUNT(*) FROM {users} WHERE name = '%s';", $name))) {
    // User doesn't exist
  }
}

It's better to query the DB directly in this case than than using user_load as it hooks into other modules as well.

googletorp
I'd forgotten you could do that with `hook_form_alter`! Kudos! Deleting my answer.
ceejayoz