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

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

2 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

4 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

5 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

5 days ago