Magento 2 “Invalid Form Key. Please refresh the page” is a disturbing error that you usually encounter when working with Magento 2.
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'
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
Go to that file and you will see path to php.ini file in php information page, sample output:
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 127.0.0.1
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 127.0.0.1 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="http://127.0.0.1:8080/"
In cause you install Magento 2 to run on HTTPS, we also change the
php bin/magento setup:store-config:set --base-url-secure="https://127.0.0.1:8080/"
If you have access to phpmyadmin, use the following command to change base url
Update baseurl in
UPDATE core_config_data SET value = 'http://www.domain2.net/' WHERE path IN ('web/secure/base_url', 'web/unsecure/base_url');
Finally, flush cache
php bin/magento cache:flush
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 magentip.com: