Magento is a vast platform with a lot of features and code correspondingly. So, the same as in any other eCommerce platform you will encounter errors.
Magento Invalid Form Key. Please refresh the page is one of the most common Magento errors you've probably come across. It can appear when you:
- install Magento 2 extension
- update Magento
- create an account on localhost
- create a configurable product with multiple child products
- add a lot of related, cross-sell and upsell products to any of the products
- save an attribute with multiple options
- assign a lot of related products and posts to a blog article
So, basically, "Magento Invalid Form Key. Please refresh the page" error appears when a big amount of data is passed to the server when you submit a form.
This guide will help you to understand the reasons behind Invalid Form Key. Please refresh the page error in Magento and how you can easily fix it.
Why "Invalid Form Key. Please refresh the page" Error Appears?
Though you can face this error while performing different tasks on your Magento installation, there are always only 3 reasons behind it.
Magento platform has certain server requirements for PHP max_input_vars value. It is the maximum number of variables the server uses for a single function to avoid overloading.
By default, it is set to 1000 which is insufficient for Magento and causes the error.
Also, you might get this issue on localhost. It appears only if you use "localhost" for the base URL instead of "127.0.0.1" and Magento simply doesn't recognize it. It causes the cookie system error and correspondingly Magento "Invalid Form Key. Please refresh the page" error.
Newer versions of Magento make the add to cart button disabled until the form _key value is available after all JS code is loaded.
Regarding the reasons, do you have any idea how to fix the "Invalid Form Key. Please refresh the page" issue?
To fix "Invalid Form Key. Please refresh the page":
Once you define the reasons for the "Magento Invalid Form Key. Please refresh the page" error to appear on your store, jump to the corresponding solution.
Solution 1. Increase the max_input_vars in PHP.
For that, you need to edit the php.ini file. Please make these changes only in case you're familiar with the php.ini file editing. Otherwise, ask your hosting provider or server admin to change that value.
- Log in to your website server using SSH.
- Find the folder with the php.ini file and open it via the vi editor.
- Edit max_input_vars to at least 5000 or 10000 and save the file.
- Restart your webserver.
Solution 2. Change the base URL of your local environment to 127.0.0.1.
You can run the following command to change it:
php bin/magento setup:store-config:set --base-url="http://127.0.0.1:8080/
In case you run Magento on HTTPS you can also change the secure base URL
php bin/magento setup:store-config:set --base-url-secure="https://127.0.0.1:8080/
To change base URL on localhost manually:
- Find the core_config_data table in your PHPAdmin.
- Edit the value of web/unsecure/base_url to 127.0.0.1.
- Flush cache to apply the changes via the:
php bin/magento cache:flush
If you encounter the "Invalid Form Key. Please refresh the page" error that appears on a storefront in the latest version of Magento, ask a developer to look into it. They have to edit the Add to Cart button template to make it available only after all JS scripts are loaded.
Note: in case you use custom extension or theme, there could be a lot of reasons and correspondingly fix routes to work upon. Check with the theme or custom extension developers.
Magento "Invalid Form Key. Please refresh the page" is a common error and you will come across it at some stage. At least now you know how to fix it so your Magento store performance is uninterrupted.