{"id":3645,"date":"2024-10-04T07:30:00","date_gmt":"2024-10-04T07:30:00","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/magento-2-admin-login-without-password-root-script\/"},"modified":"2025-03-18T10:37:15","modified_gmt":"2025-03-18T10:37:15","slug":"magento-2-admin-login-without-password-root-script","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/magento-2-admin-login-without-password-root-script\/","title":{"rendered":"How to Log into Magento 2 Admin without Password? (Using Root Script)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Ever thought if you can log into Magento 2 admin without a password? Maybe you really want to do this or want to try it as an experiment. You can use a root PHP script and log into Magento 2 admin by using the username only.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can write a custom root script to retrieve the admin user data from the UserFactory and authenticate the session. In this tutorial, I will show you how to do this, along with the fully working root script to log into Magento 2 admin without a password.<\/p>\n\n\n\n<div class=\"wp-block-simple-alerts-for-gutenberg-alert-boxes sab-alert sab-alert-warning\" role=\"alert\"><strong>Caution:\u00a0<\/strong>This method bypasses the normal user authentication and, therefore should not be used on production. It could pose several security risks.<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">\u00d7<\/span><\/button><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Magento Admin Login Without Password With Root Script<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here are the complete steps to admin login with root script in Magento 2 without using a password:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1: <\/strong>Navigate to your Magento 2 root directory (where the index.php file is present) and create a new file named <strong>adminLogin.php<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2:<\/strong> Copy the following script and paste it into the <strong>adminLogin.php<\/strong> file. Make sure to replace the <strong>$username = &#8220;admin&#8221;<\/strong> value in the script with the actual admin username in the store.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nuse Magento\\Framework\\App\\Bootstrap;\nrequire __DIR__ . '\/..\/app\/bootstrap.php';\n\n\/* code for dispaly error *\/\nini_set('display_errors', 1);\nini_set('display_startup_errors', 1);\nerror_reporting(E_ALL);\n\nclass adminLoginApp extends \\Magento\\Framework\\App\\Http implements \\Magento\\Framework\\AppInterface {\n\n    public function launch()\n    {\n        $areaCode = 'adminhtml';\n        $username = 'admin'; \/\/ admin user name '{username}'\n\n        $this->_request->setPathInfo('\/admin'); \/\/ magento admin path exam. example.com\/admin\n        $this->_state->setAreaCode($areaCode);\n        $this->_objectManager->configure($this->_configLoader->load($areaCode));\n\n        $user = $this->_objectManager->get('Magento\\User\\Model\\User')->loadByUsername($username);\n        $session = $this->_objectManager->get('Magento\\Backend\\Model\\Auth\\Session');\n        $session->setUser($user);\n        $session->processLogin();\n\n        if($session->isLoggedIn()) {\n\n            $remoteAddress = $this->_objectManager->get('Magento\\Framework\\HTTP\\PhpEnvironment\\RemoteAddress');\n            $adminSessionInfo = $this->_objectManager->create('Magento\\Security\\Model\\AdminSessionInfo');\n            $adminSessionInfo->setData('session_id', $session->getSessionId());\n            $adminSessionInfo->setData('user_id', $user->getUserId());\n            $adminSessionInfo->setData('ip', $remoteAddress->getRemoteAddress());\n            $adminSessionInfo->setData('status', '1');\n            $adminSessionInfo->save();\n\n            $cookieManager = $this->_objectManager->get('Magento\\Framework\\Stdlib\\CookieManagerInterface');\n            $cookieValue = $session->getSessionId();\n            if ($cookieValue) {\n                $sessionConfig = $this->_objectManager->get('Magento\\Backend\\Model\\Session\\AdminConfig');\n                $cookiePath = str_replace('autologin.php', 'index.php', $sessionConfig->getCookiePath());\n\n                $cookieMetadata = $this->_objectManager->get('Magento\\Framework\\Stdlib\\Cookie\\CookieMetadataFactory')\n                    ->createPublicCookieMetadata()\n                    ->setDuration(3600)\n                    ->setPath($cookiePath)\n                    ->setDomain($sessionConfig->getCookieDomain())\n                    ->setSecure($sessionConfig->getCookieSecure())\n                    ->setHttpOnly($sessionConfig->getCookieHttpOnly());\n                $cookieManager->setPublicCookie($session->getName(), $cookieValue, $cookieMetadata);\n            }\n\n            $backendUrl = $this->_objectManager->get('Magento\\Backend\\Model\\UrlInterface');\n            $path = $backendUrl->getStartupPageUrl();\n            $url = $backendUrl->getUrl($path);\n            $url = str_replace('adminLogin.php', 'index.php', $url); \/\/ adminLogin.php script file name\n            header('Location: '.$url);\n            exit;\n        }\n        return $this->_response;\n    }\n}\n\n$bootstrap = Bootstrap::create(BP, $_SERVER);\n$bootstrap = \\Magento\\Framework\\App\\Bootstrap::create(BP, $_SERVER);\n\/** @var \\Magento\\Framework\\App\\Http $app *\/\n$app = $bootstrap->createApplication('adminLoginApp');\n$bootstrap->run($app);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3: <\/strong>Now, you can use the command line to run the script or directly access the script in the browser and Magento 2 admin login using the root script.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the file name you saved is adminLogin.php, then go to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#47;&#47;your-website.com\/adminLogin.php<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll be successfully logged into the Magento admin panel using the username provided in the root script. This method is useful when you want to log into the admin panel, but have forgotten the password. However, it should be used with caution since this method can pose security issues.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for reading till the end. I hope this tutorial has helped you with Magento 2 login to the admin panel without password. In case of any queries, feel free to ask me in the comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever thought if you can log into Magento 2 admin without a password? Maybe you really want to do this or want to try it&#8230;<\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-3645","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/3645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=3645"}],"version-history":[{"count":10,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/3645\/revisions"}],"predecessor-version":[{"id":10518,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/3645\/revisions\/10518"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=3645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=3645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=3645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}