Fixing magento issues

[SOLVED] Magento 2 “Invalid Form Key. Please refresh the page”

Pinterest LinkedIn Tumblr

Magento 2 “Invalid Form Key. Please refresh the page” is a disturbing error that you usually encounter when working with Magento 2.

Magento 2 Invalid Form Key Please refresh the page

If you see this error:

  • When creating a configurable product with multiple attributes
  • When you add product to wishlist
  • When creating account or signing, add to cart on localhost
  • Even with a fresh installation of Magento 2
  • After upgrading Magento 2 version
  • Installing a new Magento 2 theme/module

Then this tutorial is for you. I will help you understand the nature of the error and certainly, solution for each.

Cause of the error and solution

PHP Max_input_vars value is not optimized for Magento 2

I have seen this error in many sites of my clients and in most cases, the cause of this error is Max_input_vars value of PHP was not properly set for Magento 2. By default, the value of PHP Max_input_vars in a server is 1000, which is not sufficient for Magento 2

PHP Max_input_vars is the maximum number of variables your server can use for a single function to avoid overloads (this value is mostly used to protect your server from DDOS attack)


In most cases, we can solve the error Magento 2 “Invalid Form Key. Please refresh the page” by increase the value of PHP Max_input_vars to the value that Magento 2 requires to handle its function

The recommended value for Max_input_vars for Magento 2 is: 100000

To change PHP Max_input_vars, you will need to access your server’s php.ini (PHP configuration file)

If you are using VPS/Dedicated server, login to your server using SSH with root access and input this command to find path to php.ini file

php -i | grep 'php.ini'

Sample output:

find path to php.ini file

So the path to php.ini is => /etc/php/7.0/cli/php.ini

If you don’t have ssh access, you can also find path of php.ini file by creating a phpinfo.php file with the following code

<?php phpinfo(); ?>

Go to that file and you will see path to php.ini file in php information page, sample output:

get php ini path phpinfo

Now edit php.ini file with a text editor and change the following value

max_input_time : 3600
max_input_vars : 100000
memory_limit : 2G

This is what php.ini file looks like after you change these values

Finally, restart apache to apply changes.


service apache2 restart


service httpd restart

Base URL on Localhost not regconize by Magento 2 system

If you are facing this issue on Localhost, probably your site are using localhost as base URL instead of

This may make fault in Magento 2 cookie system, which causes Invalid Form Key. Please refresh the page error.

We can resolve this issue by changing base url from localhost to in core_config_data table in Magento 2 database

SSH to your server and use the following command to change Magento 2 base url

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

In cause you install Magento 2 to run on HTTPS, we also change the base-url-secure :

php bin/magento setup:store-config:set --base-url-secure=""

If you have access to phpmyadmin, use the following command to change base url

Update baseurl in core_config_data:

UPDATE core_config_data
SET value = '' 
WHERE path IN ('web/secure/base_url', 'web/unsecure/base_url');

Finally, flush cache

php bin/magento cache:flush

and remove Magento 2 cache folder

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

Refresh your website to see if the error is solved


If you find this tutorial helpful, please drop a comment below to let me know if it worked and help you resolve the issue.

In case, your website still not working properly after trying my fixes, please describe in detail about the error (how you get the error, you are running your site on a live server or localhost). I will try my best to help you solve the issues.

Some other helpful guide on fixing magento 2 issues on

Hung Tran is the main editor of, he loves to write about everything related to Magento 2

Write A Comment