Drupal 7: How to reset admin password

After several research I tried the following code stored it as a php file in the root directory

saved it as password-reset-admin.php

<?php
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
require_once DRUPAL_ROOT . '/includes/password.inc';
if (isset($_GET['pass']) && !empty($_GET['pass'])) { 
$newhash = user_hash_password($_GET['pass']);
}
else {
die('Retry with ?pass=PASSWORD set in the URL');
}
$updatepass = db_update('users') 
->fields(array(
'pass' => $newhash,
// 'name' => 'admin',
// 'mail' => '<a href="mailto:yourmail@domain.com'">yourmail@domain.com'</a>;
))
->condition('uid', '1', '=')
->execute();
print "Done. Please delete this file immediately!";
drupal_exit();
 ?>

And after that access the php file through the following:

 https://yoursite.com/password-reset-admin.php?pass=newpassword

It just worked..:) Hope it helps others.

Please make sure you delete the file.

source

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading Facebook Comments ...
Loading Disqus Comments ...