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.

Files that Drupal Themes Use

A drupal theme is a collection of files that define the presentation layer. You can also create one or more "sub-themes" or variations on a drupal theme. Only the .info file is required, but most themes and sub-themes will use other files as well. The following diagram illustrates the files that are found in a typical drupal theme and sub-theme. Drupal 6: Drupal 7: .info   (required) All that is required for Drupal to see your theme is a ".info" file. Should the theme require them, meta data, style sheets ,  JavaScripts ,  block regions  and more can be defined here. Everything else is optional in drupal theme. The internal name of the theme is also derived from this file. For example, if it is named "drop.info", then Drupal will see the name of the theme as "drop".  Drupal 5 and below used the name of the enclosing folder of the theme. Info files for themes are new in Drupal 6. In version 5, .info files were used solely for dru...

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: