If you’re working with Magento 2 and come across the error message “Invalid Form Key. Please Refresh The Page,” you’re not alone.
This common issue can be frustrating, but the good news is that it’s usually an easy fix.
In this post, we’ll walk you through the steps to solve this error and get your Magento 2 site back on track. Let’s dive in!
Contents
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, adding 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.
How to Fix “Invalid Form Key. Please Refresh The Page” Error
As mentioned above, there are a few 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.
Solution 3: Upgrade Magento or Improve Website Speed
One effective way to resolve the “Invalid Form Key. Please Refresh The Page” error is by upgrading to the latest version of Magento and improving your website’s speed. This can help your site load JavaScript quickly and more efficiently.
Always ensure you are using the latest version of Magento 2. Updates often include performance improvements and bug fixes that can prevent errors like this.
Enhancing the speed and performance of your Magento store creates a more stable environment for executing JavaScript functions. Here are some tips:
- Enable Caching: Use Magento’s built-in caching mechanisms to reduce load times.
- Optimize Images: Compress and resize images to decrease load times.
- Minify CSS and JavaScript: Reduce the size of your CSS and JavaScript files.
- Use a Content Delivery Network (CDN): Distribute content across various servers to speed up load times for users around the world.
Improving the speed of your Magento store not only helps in resolving the form key error but also enhances the overall user experience, leading to a smoother and faster site.
If updating and optimizing your site doesn’t resolve the error, it’s time to consult a developer.
They can modify the Add to Cart button template to ensure it becomes active only after all JavaScript scripts have fully loaded. This adjustment can help prevent form key validation errors.
If you’re using custom extensions or themes, there might be additional causes for this error. In such cases, it’s best to contact the developers of the theme or extension for a specific fix.
Wrapping Up
Dealing with the “Invalid Form Key. Please Refresh The Page” error in Magento 2 can be frustrating, but with the right solutions, it’s a problem you can overcome. Hopefully with the solutions that we provide in this post, you can get your site back to running smoothly.
Remember, keeping your Magento store up-to-date and optimized is key to preventing such issues in the future.
If you still encounter problems, don’t hesitate to reach out to a developer for assistance, especially if you’re using custom themes or extensions.