新手求助:Drupal 7管理员账号无法登录如何解决
Hey there, let's get you back into your Drupal 7 admin account—no need to stress, we've got a few solid ways to fix this. The reason your earlier attempts to edit the users table didn't work is that Drupal 7 stores hashed passwords, not plain text. Here's what you can do:
方法1:用Drush命令(最便捷,如果你有Drush)
Drush is Drupal's go-to command-line tool, and this is the fastest fix if it's installed on your server:
- Navigate to your Drupal project's root directory via SSH or terminal
- Run this command, replacing
adminwith your actual admin username andyour-new-passwordwith the password you want:drush upwd admin --password="your-new-password" - That's it! You can now log in with the new password right away.
方法2:手动生成密码哈希并更新数据库
If you don't have Drush, you can generate a valid password hash manually and update the database:
- Generate the password hash:
- Create a new PHP file named
generate_hash.phpin your Drupal root directory, with this code:<?php require_once './includes/password.inc'; $new_password = 'your-desired-password'; $hash = user_hash_password($new_password); echo $hash; ?> - Replace
your-desired-passwordwith the password you want, then visithttp://your-site-domain/generate_hash.phpin your browser. Copy the hash string that appears on the page.
- Create a new PHP file named
- Update the database:
- Log into your MySQL database (via phpMyAdmin, command line, or your hosting control panel)
- Locate your Drupal database, open the
userstable - Find the row for your admin user (usually has
uid = 1) - Replace the value in the
passcolumn with the hash string you copied - Save the changes, then log in with your admin username and new password
方法3:临时强制登录(紧急情况用)
This is a quick workaround to get into the admin panel immediately—just remember to revert the change after you set a proper password:
- Open the
sites/default/settings.phpfile in your Drupal root directory - Add this code at the very end of the file:
function force_admin_login() { global $user; $user = user_load(1); // Replace 1 with your admin user's UID if it's different drupal_session_regenerate(); } force_admin_login(); - Save the file, then visit your website—you'll be automatically logged in as the admin
- Immediately remove this code once you're in, then go to
Account > Editto set a new, secure password
Pro tip: Always back up your database and files before making changes like this, just to be safe!
内容的提问来源于stack exchange,提问作者Abhilash

