Regaining Drupal Administrator Access via FTP or SSH
Create a login.php file in the website root with the following contents:
For Drupal 6:
http://your-site/login.php
You will be logged in as an administrator. You can then change the password if necessary.
Delete the file from the server immediately afterwards. Otherwise, anyone who runs it will gain administrator access to your website.
Back to blog
For Drupal 6:
<?php
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $user;
$user = user_load(1);
drupal_goto('admin');
For Drupal 7:
<?php
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $user;
$user = user_load(1);
drupal_session_regenerate();
drupal_goto('admin');
For Drupal 8:
<?php
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once 'autoload.php';
$kernel = new DrupalKernel('prod', $autoloader);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$user = Drupal\user\Entity\User::load(1);
user_login_finalize($user);
$response->send();
$kernel->terminate($request, $response);
Then run the file by entering the following address in your browser:
http://your-site/login.php
You will be logged in as an administrator. You can then change the password if necessary.
Delete the file from the server immediately afterwards. Otherwise, anyone who runs it will gain administrator access to your website.