The Magento Invalid Form Key. Please refresh the page error is one that most people have come across when they work with Magento 2.
There are two ways to fix this issue. Yet before we go to the solutions, let’s look at the reasons that led to this error.
Common Reasons For Magento “Invalid Form Key. Please Refresh The Page” Error
Here are the cases in which you have met this message when working on the Magento 2 admin panel:
- Creating a product with too many variables
- Adding a product to the wishlist
- Creating an account or signing, add to cart on localhost
- Installing a new theme or extension on Magento 2
- After upgrading Magento 2 version
So there are two main reasons why this happens:
- PHP Max_input_vars value is not optimized for Magento 2
The value of PHP Max_input_vars is basically the maximum number of attributes that your server can use for a function. This is set to help to avoid DDOS attacks.
The default value is set at 1000, which is obviously not enough for Magento 2, leading to the error notice.
- Magento 2 system doesn’t recognize base URL on Localhost
If you see this message on Localhost, you probably use Localhost as the base URL on your website or store (you must use 127.0.0.1 instead).
Consequently, the cookie issue on Magento will be triggered and return the Magento 2 Invalid Form Key error.
Fixing “Invalid Form Key. Please Refresh The Page” Error
As mentioned above, there are two solutions to get around this, and you can follow either of them.
Solution 1: Set the correct max_input_vars in php.ini
Because the default value of PHP Max_input_vars is only 1000, you can fix the error by increasing it to a higher number.
Follow these steps:
Step 1: Log in to the website server using ssh
Step 2: Enter
php -i | grep 'php.ini' to access the folder containing php.ini
Step 3: Change the value of
max_input_vars to 10000 (recommended) (or create it if it doesn’t exist yet). Press Esc and type
:wp to save the file.
Step 4: Restart your Mamp or Apache. After this step, you can now save products with multiple variables without any problem.
Solution 2: Change the base URL
If the issue happens on your Localhost, the solution is even simpler.
You can run this command to get rid of the error notice:
php bin/magento setup:store-config:set --base-url="http://127.0.0.1:8080/"
Or go to your PHPAdmin and search the core_config_data table, change the value of web/unsecure/base_url to 127.0.0.1:
Next, flush your cache (remove the default file based cache backend) to apply changes:
sudo rm -rf var/cache var/generation var/page_cache
So those are the solutions to fix the Magento Invalid form key. Please refresh the page error.