Skip to main content

Resetting Drupal 7 administrator password using a php file

This snippet will be helpful if you don’t have command-line access to the server to run the password-hash.sh shell file or no Drush access. In most shared hosts, command-line access is not provided.
Please keep in mind that leaving the code below on your server after resetting the password is highly critical security problem that anyone can reset your administrator password. Use this carefully.
All we are going to do is, bootstrapping Drupal, getting the necessary functions that generates the password and then updating the database with new password.
First, create a file with a random name (gh34tu9.php for example) and put the following content in it.
<?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' => 'yourmail@example.com'
  ))
  ->condition('uid', '1', '=')
  ->execute();
print "Done. Please delete this file immediately!";
drupal_exit();
?>
Save it and upload it to the root of the Drupal installation folder (where index.php, update.php, robots.txt and other files and folders exists).
Then, request the file in a browser tab in following syntax.
http://example.com/gh34tu9.php?pass=mypassword
In the above URL,
- replace example.com with your actual domain name.
- replace gh34tu9.php with the actual file name of the file that you put the above content in.
- mypassword with your new password.
Upon successful run, you will see a the text “Done” in page and password of the user/1 account (the account you created when installing Drupal) changed to the mypassword.

Comments

Popular posts from this blog

Set the default language in Notepad++

I am showing this for the PHP language. In Notepad++   Click on Settings -> Preferences   Click on the New Document tab   Change the Default Language to PHP See below inage   To add an extension to load PHP editor for different extensions.   Click on Settings -> Style Configurator   In Language scroll down to PHP and click it.   In below it will show default ext.     Add your extensions at user exit. (Don't need '.').       eg:  inc install module   Click on Save & Close See below image for reference.

Difference between session.gc_maxlifetime and session.cookie_lifetime in Drupal setting.php

ini_set('session.gc_maxlifetime', 200000); This value is for the server. It is a settings for Session Garbage Collection. If the users last visit happened before 200000s then this session is eligible for garbage collection. Since it is GC, the session value may be discarded and not compulsory. If a GC action happens after the session was made eligible for the GC, it will be deleted. ini_set ( 'session.cookie_lifetime' , 2000000 ); This value is for the browser. This is the absolute maximum time till which a browser can keep this cookie active. A 0 value here means immediate or when the browser is closed. Source: 

Difference between webform vs entityform in Drupal 7

Drupal has a lot of modules aimed at helping site builders and users add forms to their sites. What follows is a rough comparison of 2 of them. If there are any I've missed, please add them. Webform Webform is a module designed to allow you to add custom forms to the front-end of your site. Each form is stored against a node, so you add new forms to your site as if you were adding content. It's useful for things like Survey websites or just where you want a couple of forms that differ from the standard contact form. Pros Webform has been around for a long time, its very well established and has a large number of modules that  integrate with it . Webform can make a wide variety of forms with lots of different elements available out of the box. Because Webforms are nodes, they inherit all the functionality that nodes have (scheduled publishing, cloning, access control, etc.). Webforms are lighter-weight and more scalable than entity-based forms. Can handle multiple-p...