[Quick Fix] Magento 2: ‘Invalid Form Key. Please refresh the page’ error

If you found this post helpful, kindly share it and show your support :)

If you receive a “Invalid Form Key. Please refresh the page'” error after logging in to the Magento admin dashboard and are looking for a quick solution. You are in the correct location, and I have a simple fix for you.

The following are some of your activities on the Magento 2 store that led to this error:

  • After you have upgraded to the Magento 2 version.
  • When you upgrade a Magento 2 extension or theme.
  • When you design a product that can be customized and has various attributes.
  • Add to the cart on localhost when creating an account or signing up.
  • Whenever you add a product to your wish list.

You can easily fix this error by following the below steps.

  • Open the file <Magento installation directory>/vendor/magento/module-backend/App/Action/Plugin/Authentication.php

Please take a backup of the file before making any changes.

  • The following lines in the file should be commented out.
    $requestParts = explode('/', trim($request->getRequestUri(), '/'), 3);
    $baseUrlPath = trim(parse_url($this->backendUrl->getBaseUrl(), PHP_URL_PATH), '/');
    $routeIndex = empty($baseUrlPath) ? 0 : 1;
    $requestUri = $this->_url->getUrl($requestParts[$routeIndex]);
    • Add the following line to the file
    $requestUri = $this->_url->getUrl($this->_url->getStartupPageUrl());

    Here is the output:

            // Checks, whether secret key is required for admin access or request uri is explicitly set
            if ($this->_url->useSecretKey()) {
    #            $requestParts = explode('/', trim($request->getRequestUri(), '/'), 3);
    #            $baseUrlPath = trim(parse_url($this->backendUrl->getBaseUrl(), PHP_URL_PATH), '/');
    #            $routeIndex = empty($baseUrlPath) ? 0 : 1;
    #            $requestUri = $this->_url->getUrl($requestParts[$routeIndex]);
                $requestUri = $this->_url->getUrl($this->_url->getStartupPageUrl());
            } elseif ($request) {
                $requestUri = $request->getRequestUri();
            }
    
            if (!$requestUri) {
                return false;
    

    That’s it 🙂 Please refresh the Magento admin panel Dashboard and check it again.

    Loading

    Mohammed Noufal

    I'm Mohammed Noufal, working as Server Admin for the last 10 years.  In my day-to-day life, l had to face different problems related to Web-hosting. On my website Errorlogz.com, you can find solutions for different Web-hosting-related problems. Sometimes, I spent hours searching/googling to find a solution. This inspired me to start a website that gives solutions to different Webhosting problems. This website includes basic Linux and windows commands, and different control panels like cPanel, Plesk, DirectAdmin, Webmin & so on. You can find solutions and or suggestions for different Web-hosting related problems from here. Hence I would like to say Errorlogz is your server protector.  I will be glad if Logdetect can help any one of the Server admins to find a solution to his problem.

    You may also like...

    Leave a Reply

    Your email address will not be published. Required fields are marked *