Magento Tutorials

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

Sometimes when you create a product with multiple attributes in Magento 2, you may end up with the error message “Invalid Form Key. Please refresh the page.

Magento invalid form key error

Here are ways you can solve this issue:

Solution 1: Increasing max_input_vars in php.ini

By default, the value of max_input_vars in php.ini is set to 1000. You’ll want to set this to 10000. To edit your php.ini file, follow these steps:

  1. Log in to your website server using ssh:
magento server login

2. Go to the folder containing php.ini and open this file by vi

php.ini

3. Edit the value of max_input_vars to 10000 (or create it if it doesn’t exist yet). Then, press ESC key and type :wq to save the file.

max_input_vars

4. Restart your Mamp or Apache. Now you can save your newly created product without any problem.

Solution 2: Changing your base URL

Another possible reason for this issue is because your base URL is not recognized by the Magento 2 system. This is most commonly seen in local environments (localhost). To get around this, run 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/"

Or go 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:

Change web/unsecure/base_url

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

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

To learn more knowledge about Magento 1 & 2, you can read our other helpful tutorials.

Recommending Reading:
Guide to Enable/Disable or Clear Cache in Magento 2
Magento 2 Error: One or more indexers are invalid