Reset the Drupal 7 administrator password

Public

Create a file with random name, put it in the root and then call it via url http://example.com/gh34tu9.php?pass=mypassword, but first, create this file:

</> CopyGet raw version
php
  1. <?php
  2. define('DRUPAL_ROOT', getcwd());
  3. require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  4. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  5. require_once DRUPAL_ROOT . '/includes/password.inc';
  6. if (isset($_GET['pass']) && !empty($_GET['pass'])) {
  7. $newhash = user_hash_password($_GET['pass']);
  8. }
  9. else {
  10. die('Retry with ?pass=PASSWORD set in the URL');
  11. }
  12.  
  13. $updatepass = db_update('users')
  14. ->fields(array(
  15. 'pass' => $newhash,
  16. // Uncomment the following lines to reset the administrative username and/or email address, if necessary.
  17. // 'name' => 'admin',
  18. // 'mail' => 'yourmail@example.com'
  19. ))
  20. ->condition('uid', '1', '=')
  21. ->execute();
  22. print "Done. Please delete this file immediately!";
  23. drupal_exit();
  24. ?>

Comments

Alexei's picture

Easier with drush user-password admin --password=secret command.