Magento: Programatically login to Magento admin section by username

Programatically Auto login to Magento admin section by username

Create a PHP file with below script and access in the URL

You will be automatically logged in to admin section and redirected to start up page

This script is very usefull when you forgot your Magento admin password or if you are building an application that requires autologin feture.

[php]
<?php
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
Mage::app();
umask(0);
Mage::app('default');

Mage::getSingleton('core/session', array('name' => 'adminhtml'));

$user = Mage::getModel('admin/user')->loadByUsername('admin'); // Here admin is the Username
if (Mage::getSingleton('adminhtml/url')->useSecretKey()) {
Mage::getSingleton('adminhtml/url')->renewSecretUrls();
}

$session = Mage::getSingleton('admin/session');
$session->setIsFirstVisit(true);
$session->setUser($user);
$session->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());

Mage::dispatchEvent('admin_session_user_login_success',array('user'=>$user));

if ($session->isLoggedIn()) {

$redirectUrl = Mage::getSingleton('adminhtml/url')->getUrl(Mage::getModel('admin/user')->getStartupPageUrl(), array('_current' => false));
header('Location: ' . $redirectUrl);
exit;

}

[/php]