The Most Popular Extension Builder for Magento 2

With a big catalog of 224+ extensions for your online store

How to Fix Magento 2 'Invalid Form Key. Please refresh the page' error

Magento stands out as a comprehensive platform boasting numerous features and corresponding code. As is the case with any eCommerce platform, encountering errors is an inherent aspect.

Among the most prevalent Magento errors is the “Magento Invalid Form Key. Please refresh the page.” This error may surface in various scenarios, such as when you:

  • Install a Magento 2 extension

  • Update the Magento platform

  • Create an account on the localhost

  • Establish a configurable product containing multiple child products

  • Include a substantial number of related, cross-sell, and upsell products to any given item

  • Save an attribute with multiple options

  • Allocate numerous related products and posts to a blog article

  • In essence, the “Magento Invalid Form Key. Please refresh the page” error emerges when a substantial volume of data is transmitted to the server during form submission.

Indeed, this detailed guide is for you. In this tutorial, we will help you understand the cause of this error as well as the solutions to fix each error.

Without further ado, let’s get started!

Table of Contents

The reasons occur Magento 2 “Invalid Form Key. Please refresh the page” error


1. PHP Max_input_vars value is not optimized for Magento 2

We have encountered this error a lot on customer sites, even on our Magento 2 site before. The cause of the date error is that the Max_input_vars value is not set correctly for Magento 2. By default, the value of PHP MAx_input_vars in the server is 1000; this means it is not enough for Magento 2.

PHP Max_input_vars can be understood as the maximum number of variables that your server can use for a single function to avoid overloading (this value is used mainly to protect your server from DDOS attacks)

2. Base URL on Localhost not recognized by Magento 2 system

If you are facing this error on Localhost, we are guessing that you are probably using Localhost as the base URL on your website, instead of

Surely, this will cause cookie system error on Magento and generate Magento 2 Invalid Form Key. Please refresh the page on your website.

3. An outdated Magento version or an old form

You might also encounter this error on a storefront when attempting to submit certain forms. This problem arises in cases where you are using an outdated version of Magento or an older form, and not all JavaScript is loaded for the intended action (e.g., adding items to the cart).

In more recent Magento versions, the add to cart button remains disabled until the necessary form _key value is accessible, ensuring that all JavaScript code is fully loaded before the action is executed.

How to fix Magento 2 Invalid Form Key. Please refresh the page

With 2 main causes of this error as the content mentioned above. We will give 2 corresponding solutions for them. Let’s dive into each solution right away.

Solution 1: Increase max_input_vars in php.ini

In most cases, we can solve the Magento 2 error “Invalid Form Key. Please refresh the page” by increasing the value of PHP Max_input_vars to the value that Magento 2 requires to process its functionality.

  • The suggested value for Magento 2 Max_input_vars is 10000.

  • To edit the value of PHP Max_input_vars, you need to access php.ini on your server.

Follow the below steps carefully to change PHP Max_input_vars.

Step 1: Please log in to your server using SSH with root access if you are using VPS/Dedicated server.


Step 2: Enter this command php -i | grep 'php.ini' to find the path to the php.ini folder. You will see the output sample below.


Step 3: Change the value of Max_input_vars to 10000 (or you can also create if it doesn’t exist yet). Then, press the ESC key and type :wq to save this folder.

Magento 2 Invalid Form Key. Please refresh the page

Step 4: Last but not least, restart your Apache. Now you can save a newly created product without facing any problem.

Solution 2: Change your base URL

Another possible reason for this error is because the Magento 2 system does not recognize your base URL. It usually happens in a Local environment (localhost). To solve this problem, it is straightforward, you just need to change the base URL to “”.

php bin/magento setup:store-config:set --base-url=""

Also, you can go to your PHPAdmin and search the following table: core_config_data, then, edit the value of web/unsecure/base_url to


If you install Magento 2 to run on HTTPS, you can change the base-url-secure :

php bin/magento setup:store-config:set --base-url-secure=""

Finally, after you have changed your base URL, it’s time to flush your cache to save changes by running the following command:

php bin/magento cache:flush

And remove Magento 2 cache folder by running the command below:

sudo rm -rf var/cache var/generation var/page_cache

Last but not least, please refresh your website to see whether the error is fixed.

Solution 3: Regularly update Magento version or improve Magento speed

If you come across the “Invalid Form Key. Please refresh the page” error on a storefront in the latest Magento version, it’s advisable to consult a developer for resolution. They need to modify the Add to Cart button template, ensuring it becomes active only after all JavaScript scripts are fully loaded.

Note: If you’re using a custom extension or theme, there could be various reasons for the issue, and corresponding fixes may be required. It’s recommended to reach out to the developers of the theme or custom extension for assistance.

The “Invalid Form Key. Please refresh the page” error is common in Magento, and having an understanding of how to address it ensures uninterrupted performance for your Magento store.

Solution 4: Adjust a file in the Magento installation directory

Begin by creating a backup of **/vendor/magento/module-backend/App/Action/Plugin/Authentication.php.**

Next, follow the steps outlined below:

  • Open the file.

  • Disable the specified entries:

$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 entry:
$requestUri = $this->_url->getUrl($this->_url->getStartupPageUrl()); 
  • The modified section should appear as follows:
// 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;

Final Words

That’s all. We have just provided detailed instructions to fix the Invalid Form Key error. Please refresh the page in Magento 2.

We hope that you can solve the problem yourself by following the step-by-step instructions above. If your Magento 2 website still doesn’t work properly after applying the above tutorial, you can comment below on the details of the error; our support team will try their best to help you solve it as soon as possible.

Also, don’t forget to share this article with your friends if you find it helpful!

Thanks a lot for reading, and see you in the next post.

Image Description
A data-driven marketing leader with over 10 years of experience in the ecommerce industry. Summer leverages her deep understanding of customer behavior and market trends to develop strategic marketing campaigns that drive brand awareness, customer acquisition, and ultimately, sales growth for our company.

Looking for
Customization & Development Services?

8+ years of experiences in e-commerce & Magento has prepared us for any challenges, so that we can lead you to your success.

Get free consultant
development service

    Explore Our Products:


    Stay in the know

    Get special offers on the latest news from Mageplaza.

    Earn $10 in reward now!

    Earn $10 in reward now!

    go up