Magento 2: Invalid Form Key. Please refresh the page. Error on Product Save

Table of Contents

Problem:

The following error occurred while saving a product from Magento 2 admin:

Invalid Form Key. Please refresh the page.

Cause:

The cause of this error is related to the max_input_vars (Maximum Input Variables) directive in the PHP settings.

The max_input_vars directive value is the maximum number of variables that can be used for a single function.

By default, the value of max_input_vars is 1000.

Solution:

The value of max_input_vars should be increased to solve the error.

To increase the max_input_vars value, we need to edit the php.ini file.

You can find the path of the php.ini file with the following command:


php -i | grep php.ini

Output:


Configuration File (php.ini) Path => /usr/local/php5/lib
Loaded Configuration File => /usr/local/php5/lib/php.ini

Open php.ini file and increase the value of max_input_vars:


; How many GET/POST/COOKIE input variables may be accepted
 max_input_vars = 5000

Save the php.ini file and then restart your webserver.

If you are using Apache WebServer, then you can restart it with the following command:

Mac OS:


sudo /usr/sbin/apachectl restart

Linux (Debian/Ubuntu):


sudo /etc/init.d/apache2 restart

//OR

sudo service apache2 restart

Linux (CentOS):


sudo /sbin/service httpd restart

//OR

sudo systemctl restart httpd.service

Hope this helps. Thanks.