How to Fix Magento 2 'Invalid Form Key. Please refresh the page' error
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
- How to fix Magento 2 Invalid Form Key. Please refresh the page
- Final Words
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 127.0.0.1.
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_varsis 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
- Please log in to your server using SSH with root access if you are using VPS/Dedicated server.
- Enter this command
php -i | grep 'php.ini'to find the path to the php.ini folder. You will see the output sample below.
- Change the value of
Max_input_varsto 10000 (or you can also create if it doesn’t exist yet). Then, press the ESC key and type
:wqto save this folder.
- 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 “127.0.0.1”.
php bin/magento setup:store-config:set --base-url="http://127.0.0.1:8080/"
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 127.0.0.1:
If you install Magento 2 to run on HTTPS, you can change the
php bin/magento setup:store-config:set --base-url-secure="https://127.0.0.1:8080/"
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.
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.
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