[Drupal] How to reset the user credentials in Drupal site

| | 1 min read

Forgetting a password is a usual scenario. The first thing that may come to your mind when you forget the password of your site is resetting it. If it is a live site, then we can ask to send a new password by mail. But what is if it is your local site where no mails are configured? well, I know a trick or two to solve this programatically. If you too want to know, then read on.

Step 1 : Create a new file and copy the below script in that.


  define('DRUPAL_ROOT', getcwd());
  require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
 
  require_once 'includes/password.inc';
  echo user_hash_password('admin');
 
  die();

Step 2 : Replace 'admin' with the user name for which you want to reset the password.

Step 3 : Save this file in your root Drupal folder and name it as reset_password.php.

Step 4 : Execute this file in your server, as a result you may get a hash code, for example something like :
$S$DMNPfKMxzmGB6wQZtxuVDF3uSqZef8aBRrUJLKfJHdXwfaUjh3zB

Step 5 : Login to your database via PHPMyAdmin tool, browse the 'users' table.

Step 6 : Edit the admin entry, copy the above hash code (obtained from step4) and paste it in 'pass' field of the admin entry and save the changes.

Step 7 : You may now login with the new password.

Step 8 : Don't ever forget to remove the file with the above script.

Hope this article saved your day!!