Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change admin password in drupal 7

I am using built in Drupal 7 user module, fore user registration, forgot-your-password-emails and all that stuff.

I have forgotten my admin password. I have access to my website which is hosted on 1and1.com and also have access to mysql?

Is it possible to change password or email address through SQL so that I can access the admin page?

If it possible how? Can you somebody help me with this?

Thanks!

like image 931
TheDevMan Avatar asked Nov 30 '22 11:11

TheDevMan


2 Answers

If you have Drush installed, you just have to enter the following command in the terminal from anywhere inside the site root.

drush upwd admin --password=mynewpassword

Here, admin is the user name; who's password will be changed to mynewpassword.

like image 94
Ajit S Avatar answered Dec 29 '22 00:12

Ajit S


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:[email protected]'">[email protected]'</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.

like image 28
TheDevMan Avatar answered Dec 29 '22 00:12

TheDevMan