Checking whether a user already exists in drupal
Asked Answered
S

5

14

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.

Suk answered 10/5, 2010 at 14:32 Comment(0)
G
22

Drupal 7 provides a function to get a user object by name :

$user = user_load_by_name($name);
if(!$user){
    // User doesn't exist
} 
else {
    // User exists
}

http://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_load_by_name/7

Gaytan answered 28/9, 2012 at 7:18 Comment(2)
This is a heavy-handed solution as it loads the entire user object, just to validate the existence of a user by name. Also, if you're loading a user, do not use a variable named $user as it could easily be confused with the global $user which is the current user account.Berceuse
you can also use user_load_by_mail in case you're searching by mail: api.drupal.org/api/drupal/modules!user!user.module/function/…Carmel
E
12

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'];
  // Drupal 6:
  if (!db_result(db_query("SELECT COUNT(*) FROM {users} WHERE name = '%s';", $name))) {
    // User doesn't exist
  }
  // Drupal 7:
  if (!db_query("SELECT COUNT(*) FROM {users} WHERE name = :name;", array(':name' => $name))->fetchField()) {
    // 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.

Elinoreeliot answered 10/5, 2010 at 15:30 Comment(2)
I'd forgotten you could do that with hook_form_alter! Kudos! Deleting my answer.Preset
This does not work for Drupal 7. This does: $result = db_query("SELECT COUNT(*) FROM {users} WHERE uid = :uid", array(':uid' => $uid))->fetchField(); if ($result == 0) { // User doesn't exist return FALSE; } return TRUE;Joycelynjoye
L
3

In Drupal 7, substitute for this in the validation function:

if (!db_query("SELECT COUNT(*) FROM {users} WHERE name = :name", array(':name' => $name))->fetchField()) {
  // User doesn't exist
}
Lanza answered 27/6, 2011 at 11:57 Comment(0)
G
1

I realize this is almost 2 years old, but user_authenticate does this nicely.

$existing_user = user_authenticate($name,$password);
if($existing_user)
  // user exists
else
  // user doesn't exist

Hope this helps someone else.

Gitt answered 24/3, 2014 at 12:18 Comment(2)
this needs the password, this is a bad answer.Joycelynjoye
If the user is entering a username and password why would this be a bad solution? The password isn't getting displayed and I'm simply using the password that was given. Am I missing something?Gitt
S
1

You can try to look on these 2 modules for inspiration: friendly_register and username_check.

Shallot answered 15/9, 2015 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.