Checking whether a user already exists in drupal

simonr picture simonr · May 10, 2010 · Viewed 20.3k times · Source

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.

Answer

Mika Andrianarijaona picture Mika Andrianarijaona · Sep 28, 2012

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