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

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: 

Smart pagination or page break in Drupal 7(CK editor)

1. Install Smart Paging module   Go to Administration › Configuration › Administer Smart Paging settings.   Under 'Default page break method', select "Manual placement of page break placeholder". 2.  Install  'Ckeditor' Module   Go to Administration › Configuration > Ckediotr profiles > Filtered HTML   Edit the settings of the Advanced (Filtered HTML) Profile. Under 'Editor Appearance' section, In plugins check the required options like " Plugin for inserting a Drupal teaser and page breaks. ". 3.  Edit the configuration settings of input formats (Filtered HTML, Full HTML, Plain Text)   Go to Administration › Configuration > Text formats. Edit the required input format. For example say "Filtered HTML". Under  "Enabled filters" section, check the 'Smart Paging' option and uncheck all the remaining checkboxes. 4. Go to Content type 'article' and create new content. We will s...

Smart pagination or page break in Drupal(wysiwyg)

1)Install Smart Paging module   Go to Administration › Configuration › Administer Smart Paging settings.   Under 'Default page break method', select "Manual placement of page break placeholder". 2)Install "wysiwyg" module and 'ckeditor' profile   Go to Administration › Configuration > Wysiwyg profiles > Filtered HTML   Edit the settings of Filtered HTML Profile. Under 'Buttons and plugins' section, check the required options like "Smart Paging ","Image","Bold". 3)Edit the configuration settings of input formats(Filtered HTML, Full HTML, Plain Text)   Go to Administration › Configuration > Text formats. Edit the required input format. For example say "Filtered HTML". Under  "Enabled filters" section, check the 'Smart Paging' option and uncheck all the remaining checkboxes. 4)Go to Content type 'article' and create new content. We will see the wy...