Magento Tutorials

How to fix “Invalid Form Key. Please refresh the page” error in Magento 2

Hello Magento Folks,

In this tutorial, I am going to provide the solution for the most common error in Magento 2, “Invalid Form Key. Please refresh the page”. 

Everyone might have encountered an Invalid form key error while working with Magento admin. Today I am here to resolve this error in two ways. Before that, first, let’s understand the reasons behind the error.

Actions that cause “Invalid Form Key. Please refresh the page” error in Magento 2

The following actions may be the cause for “Invalid Form Key. Please refresh the page” error:

  • Use of multiple attributes to create products
  • While the product is added to the wishlist
  • Installation of new extension or theme
  • Upgrading Magento 2 version
  • Account creation or signing
  • Add to cart on localhost
  • Error in the custom module
  • Difficulty in redirection

Reasons for “Invalid Form Key. Please refresh the page” error in Magento 2

There are two main reasons that generate “Invalid Form Key. Please refresh the page” error.

PHP Max_input_vars value is not optimized for Magento 2

PHP Max_input_vars is the maximum number of attributes that your server can use for a function. The default value is 1000 which is not enough for Magento 2. This causes the invalid form key error.

Base URL on Localhost not recognized by Magento 2 system

If you encounter an invalid form key error on localhost, it is probably because you will be using localhost as the base URL for your website instead of 127.0.0.1

How to fix “Invalid Form Key. Please refresh the page” error in Magento 2

There are 2 ways to fix “Invalid Form Key. Please refresh the page” error in Magento 2

Solution 1: Increase max_input_vars value in php.ini file

Go to path – /etc/php/7.3/apache2

Now, open your terminal and run the below command

gedit php.ini

Change max_input_vars to 5000 as shown in the below image.

Please check the updated value by phpinfo file.

Solution 2: Modify your base URL

Run the below command  to get rid of the invalid form key error

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

Or else you can follow the below steps,

  • Go to your PHP admin
  • Find the core_config_data table
  • Change the value of web/unsecure/base_url to http://127.0.0.1

Final Words:

Try the above solutions to get rid of the “Invalid Form Key. Please refresh the page” error in Magento 2. If the error still persists, you can take the help of our Support Team which will guide you to solve the invalid form key error.

Spread the article amongst your friends to help them resolve the error.

Happy Reading!

Click to rate this post!
[Total: 7 Average: 4.4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

  • The first option was useless because my limit was already high. The second option broke my login page.

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago