Magento invalid form key. please refresh the page: How to solve this error in Magento 2

This error message “Magento invalid form key. please refresh the page.” appears in Magento 2 because of the limited number of variables that can be used for a single feature. This post today, Magesolution will show you the nature of the error and certainly, solutions to deal with these issues.

Cause of the error: Magento invalid form key. please refresh the page

Magento invalid form key. please refresh the pag

There are some reasons behind ” Magento invalid form key. please refresh the page ” error message which is:

  •  When a developer uses multiple attributes to generate products, the invalid form key hits the page.
  • Sometimes there is a mistake in the custom module, ‘Magento invalid form key. please refresh the page ” display on your screen.
  • Your code is redirecting the user to another page. And while redirecting, any difficulty displays, from the key error that comes up.
  • Cookies are also the reason behind the invalid key.
  • Magento server configuration also plays an important role in the  ‘Magento invalid form key. please refresh the page ”  error.

The methods to solve this error

There are a few solutions out there to fix the issue. One of the solutions is to change your base URL

Method 1: Change your base URL

Another possible reason for this error is that your base URL is not recognized by the Magento 2 system. This is most popular seen in local environments (localhost). To deal with it, you can follow this command to change your base URL to “127.0.0.1”:

php bin/magento setup:store-config:set --base-url="http://127.0.0.1:8080/"

After that, you move to your PHPAdmin and find the following table: core_config_data, and change the value of web/unsecure/base_url to 127.0.0.1

Magento invalid form key. please refresh the page

After changing your base URL, make sure to flush your cache to apply changes:

sudo rm -rf var/cache var/generation var/page_cache

To learn more knowledge about Magento you can have a look at our other helpful tutorials.

Method 2: Increasing max_input_vars in php.ini

In Magento default, the value of max_input_vars in php.ini is set to 1000. if you want to set this to 10000. To edit your php.ini file, follow the below steps:

  • Log in to your server by using ssh:
magento-invalid-form-key-please-refresh-the-page
  • Move to the folder containing php.ini and open this file by vi
Magento invalid form key. please refresh the page
  • After that, you edit the value of max_input_vars to 10000 (or create it if it doesn’t exist yet). Then, you have to press ESC key and type :wq to save the file.
  • Lastly, you need to restart your Mamp or Apache. Now you can save your newly created items without any problem.

Solution for in some cases

Multiple Attributes in Single Product issue in Magento 2:

In this case, you can follow these steps to set products attributes in bulk – Magento 2

  •  Log in to Admin.
  • Choose Catalog → Products.
Magento invalid form key. please refresh the page
  • Select Products to change attributes.
Magento invalid form key. please refresh the page
  • As shown in the image, after choosing products, click on the drop-down menu and choose the Update attributes option.
Magento invalid form key. please refresh the page
  • After choosing the update attribute option, you need to click on the change checkbox, and update attributes.
Magento invalid form key. please refresh the page

Custome module Issue

For custom modules error, correction must be required first to handle this issue. After the correction, please refresh the page.

Redirection to another page

When you are redirecting the user to another page, ensure about the link/ mentioned URL of the page. Check and refresh the page to handle the problems of the form key.

Cookies issue

Clear or delete cookies as well as browser cache must be cleared too. So it may happen that you have any trouble with the “Invalid form key, please refresh the page” error.

DELETE FROM core_config_data WHERE path='web/cookie/cookie_domain'

Magento Server Configuration

Increase Hosting Provider to 70000 to 10000: php_value max_input_vars 100000

» If app/code/local/Mage/Core/Model/Session.php exists, remove it.

» Open var/cache directory and clean it.

Conclusion

That’s all about the “Magento invalid form key. please refresh the page” error in Magento 2.  We hope that you found this tutorial helpful. However, If you still find it difficult or have any further questions regarding Magento, then contact us and our Magento customization services to get an instant solution.

Magento gets current URL: Methods to get current url and base URL in Magento 2