How to login as any user in Drupal site?
It is no surprise that production sites at times may work differently from dev / stage due to various factors. And in such circumstances, there is a need to login as real site user to be able to reproduce the issue. Of course it is not easy to fix a bug unless it is really reproduced by developer.
In one of my recent works, I found a simple trick using Drush to get access to real user account without distrubing his/her account credentials like login name, email, password, or similar.
The trick I used counts on drush user-login command, alias uli (which I learned from my earlier blog post's comments).
Assume sivaji (login name) is a real user in your Drupal site, and you are really in need to get access to this user account, then issue the below command,
$ drush uli sivaji
The output of the command will be a link, which you need to open in browser to get access of mentioned user account. The sample URL could be as below,
Make sure you are opening as anoymous user. If you are in multisite environment, the above command may not work, check Using Drush to administer multisite for the same.
Check out help for more advanced usage, as the above command can work with uid and email as well.
$ drush help uli
Display a one time login link for the given user account (defaults to uid 1). Examples: drush user-login ryan node/add/blog Displays and opens default web browser (if configured or detected) for a one-time login link for the user with the username ryan and redirect to the path node/add/blog. drush user-login --browser=firefox Open firefox web browser, login as the user with the e-mail address firstname.lastname@example.org and redirect to the path --email@example.com admin/settings/performance. admin/settings/performance Arguments: user An optional uid, user name, or email address for the user to log in as. Default is to log in as uid 1. The uid/name/mail options take priority if specified. path Optional path to redirect to after logging in. Options: --browser Optional value denotes which browser to use (defaults to operating system default). Set to 0 to suppress opening a browser. --mail A user mail address to log in as. --name A user name to log in as. --uid A uid to log in as. Aliases: uli
Of course there is a flip side here! This resets the last login time :(
If this is not expected for your site's requirements, be prepared to workaround or refrain from using this trick.
Hope this helps. Happy Drupal hacking!