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

The Most Popular Extension Builder for Magento 2

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

If you are searching for “How to fix the error: Magento 2 Invalid Form Key. Please refresh the page” or “How to resolve Magento 2 Invalid Form Key. Please refresh the page”, yes, you have come to the correct spot.

This is not a particular bug in Magento, even though it is widespread. I’m sure everyone has encountered this error before. Here are some of your actions on the Magento 2 store that cause this error:

  • When you create a configurable product with multiple attributes
  • When adding a product to the wishlist
  • When you create an account or signing, add to cart on localhost
  • When you install a new extension/ theme on Magento 2
  • After you upgrade Magento 2 version

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.

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.

  1. Please log in to your server using SSH with root access if you are using VPS/Dedicated server.
  1. Enter this command php -i | grep 'php.ini' to find the path to the php.ini folder. You will see the output sample below.
  1. 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

  1. 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.

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.

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