How To Bypass Magento 2.X Admin Login Page With Help Of Username & Admin URL.

Nimish Agarwal
3 min readMar 1, 2023

In this article, we discussed about how to bypass magento 2.X admin login by just getting admin username and admin url.

For Bypass Admin Login Page, Follow the below steps to directly auto login in magento 2.X admin dashboard.

Before move forward, take two things handy.
1) Admin Username. i.e. root
2) Admin URL of Magento 2.X Store. i.e. https://example.com/{admin}

Lets follow the below steps to achieve the goal of this article and directly access to the dashboard of magento 2.X.

Step 1: Go to your magento 2.X pub directory.

Example Path: {Your_Magento_Root}/pub

Step 2: Create a file, example_name.php. i.e In our case, our file name is adminLogin.php

Step 3: Copy & Paste below code in above newly created file on pub directory & save it.

Note: Enter details & follow comments according to the commented line in below code.

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';

/* code for dispaly error */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

class adminLoginApp extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface {

public function launch()
{
$areaCode = 'adminhtml';
$username = 'admin'; // admin user name '{username}'

$this->_request->setPathInfo('/admin'); // magento admin path exam. example.com/admin
$this->_state->setAreaCode($areaCode);
$this->_objectManager->configure($this->_configLoader->load($areaCode));

$user = $this->_objectManager->get('Magento\User\Model\User')->loadByUsername($username);
$session = $this->_objectManager->get('Magento\Backend\Model\Auth\Session');
$session->setUser($user);
$session->processLogin();

if($session->isLoggedIn()) {

$remoteAddress = $this->_objectManager->get('Magento\Framework\HTTP\PhpEnvironment\RemoteAddress');
$adminSessionInfo = $this->_objectManager->create('Magento\Security\Model\AdminSessionInfo');
$adminSessionInfo->setData('session_id', $session->getSessionId());
$adminSessionInfo->setData('user_id', $user->getUserId());
$adminSessionInfo->setData('ip', $remoteAddress->getRemoteAddress());
$adminSessionInfo->setData('status', '1');
$adminSessionInfo->save();

$cookieManager = $this->_objectManager->get('Magento\Framework\Stdlib\CookieManagerInterface');
$cookieValue = $session->getSessionId();
if ($cookieValue) {
$sessionConfig = $this->_objectManager->get('Magento\Backend\Model\Session\AdminConfig');
$cookiePath = str_replace('autologin.php', 'index.php', $sessionConfig->getCookiePath());

$cookieMetadata = $this->_objectManager->get('Magento\Framework\Stdlib\Cookie\CookieMetadataFactory')
->createPublicCookieMetadata()
->setDuration(3600)
->setPath($cookiePath)
->setDomain($sessionConfig->getCookieDomain())
->setSecure($sessionConfig->getCookieSecure())
->setHttpOnly($sessionConfig->getCookieHttpOnly());
$cookieManager->setPublicCookie($session->getName(), $cookieValue, $cookieMetadata);
}

$backendUrl = $this->_objectManager->get('Magento\Backend\Model\UrlInterface');
$path = $backendUrl->getStartupPageUrl();
$url = $backendUrl->getUrl($path);
$url = str_replace('adminLogin.php', 'index.php', $url); // adminLogin.php script file name
header('Location: '.$url);
exit;
}
return $this->_response;
}
}

$bootstrap = Bootstrap::create(BP, $_SERVER);
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('adminLoginApp');
$bootstrap->run($app);

Step 4: Save the code after successfuly edited by following commented lines in code.

Step 5: Now, Hit the url of that file in the browser of your choice.

Example Url: https://example.com/{your_file_name}.php
In our case, our path is: https://example.com/adminLogin.php

Dashboard Directly Login!

Code File Reference: Click Here
OR
https://drive.google.com/file/d/1PZKMr4KmPIylQexbupfzLU25YJXuYFvW/view?usp=share_link

Conclusion:

After complete following all the steps, you are able to directly login your magento 2 dashboard or automatically login your magento 2 dashboard with the username that you enter in the code.

Magento 2 Admin Login Page Bypass Successfully!

Thanks for the Reading :)

--

--