27

I am unable to save the product in Magento 2.

It shows "Invalid Form Key. Please refresh the page. in local

enter image description here

how to resolve this?

5
  • 2
    Please check your cookie setting
    – Amit Bera
    Commented Feb 20, 2017 at 14:56
  • setting max_input_vars to 10000 in php.ini resolved my issue. Commented Sep 11, 2018 at 12:05
  • Have you tried using different browser? I usually use Google Chrome but came across this problem, setting max_input_vars to higher values didn't help, neither using local IP address instead of localhost, but I tried using Firefox and it worked. Commented Jan 7, 2019 at 7:21
  • see this github.com/magento/magento2/issues/25427
    – Waqar Ali
    Commented Nov 14, 2019 at 10:41
  • 1
    Issues has been resolved by setting below values in php.ini max_input_vars = 10000 max_input_time = 600 max_execution_time = 3000 Commented Jun 10, 2020 at 5:38

15 Answers 15

37

This bug arises due to php.ini variable "max_input_vars" Increase it to 10000 through below php function:

ini_set('max_input_vars', 10000);

OR Using linux command line

cd /etc/php/7.0/apache2/
vi php.ini (Open for edit and set max_input_vars=10000)
:wq(Press esc and write the mentioned command to save and exit)
sudo /etc/init.d/apache2 restart(Restart Server)
8
  • 1
    Thanks for you replay @ahmad vaqas khan let me check and update you thanks... Commented Apr 21, 2017 at 8:29
  • ok. keep updated regarding it. Commented Apr 21, 2017 at 10:48
  • 1
    It worked for me. Very interesting though. How did you debug that? Commented Aug 7, 2017 at 13:53
  • @AbhishekJakhotiya : Great . I googled and came across this useful info . Often being PHP Core deveoper is quite helpful along with Magento developer . Commented Aug 7, 2017 at 17:59
  • 1
    I have changed max_input_vars to 10000, but still having same issue. I am working on local (using ubuntu 16.04) and magento 2.3 Commented Dec 21, 2018 at 5:51
12

This problem is occurred after adding more custom options and categories in the product so because php.ini file not allowed to add more custom options.

So, we need to change default settings of php.ini file.

If php version 7.1 then,

/etc/php/7.1/apache2/php.ini

If php version 7.0 then,

/etc/php/7.0/apache2/php.ini

If php version 7.2 then,

/etc/php/7.2/apache2/php.ini

Uncomment this value and update this all value in php.ini,

max_input_vars = 200000
max_input_time = 600
max_execution_time = 3600

Then Restart Apache 2 web server, and check it.

sudo service apache2 restart
8

If you are running this Magento site on a local machine (local host) as your question seems to indicate, you may have trouble using the: http://localhost/ syntax, as Magento 2 doesn't fully know how to deal with cookie.

Instead, I'd recommend using http://127.0.0.1/ to access the local site; in this case your browser should be able to recognize this more easily and work with Magento's cookies properly.

You may need to set a cookie domain in the admin area of Magento.

I hope this helps...

3

After endlessly trying all the suggested tweaks to php.ini and cleaning cache, my magento2 still wouldn't accept cookies from localhost even if I used the linux machine name instead, making it seem like a domain name. i finally resorted to using localho.st which is a domain name that resolves dns to your local ip address and this is what worked.

2
  • 1
    That is the perfect reason i was stuck in. I keep my localhost name in single word without any Dot, as you described. Once localhost name was split into 2 by a Dot in between, the problem is fixed now. Commented Nov 3, 2021 at 13:51
  • 1
    i’m happy to have spared you the time, it took me the better part of a day to fix this… kept doing everything I read and his was the only thing that finally worked
    – Vlad A.
    Commented Nov 4, 2021 at 14:12
2

Try This Ubuntu 14.0.4 System in magento 2.3-invalid Form key error.

I face invalid Form key error on frondend-side insertion, But crome browser only occured this error, Insertion correctly run in firefox browser.

I solved this error Following steps:-

Change your base URL from localhost to 127.0.0.1.

Using localhost as the domain in your base URL causes problems in Magento 2.

Magento's base URL configuration is located in the core_config_data table. The following values may need to be changed:

web/unsecure/base_url web/secure/base_url For example, if you previously had http://localhost/magento2community/, you should change it to http://127.0.0.1/magento2community/

After changing the url, flush the cache (bin/magento cache:flush). You might also need to clear out your var directories.

I find this solution through this Like https://magento.stackexchange.com/a/122167/78151

2

There are 2 possible causes of this error: PHP Max_input_vars value is not optimized for Magento 2 In most cases, we can solve the error by increase the value of PHP Max_input_vars to the value that Magento 2 requires. Open PHP.ini and change these values

max_input_time : 3600
max_input_vars : 100000
memory_limit : 2G

Base URL on Localhost not regconize by Magento 2 system If you are running Magento 2 on local environment, change baseurl to 127.0.0.1 should resolve this issue. Run this command

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

Also flush cache to apply changes

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

Reference source: https://magentip.com/solved-magento-2-invalid-form-key-please-refresh-the-page/

0

In my case it was due to the website protection settings of Firefox.

Firefox blocked some cookies that were needed by Magento2.

So I had to disable it by going to Preferences -> Privacy & Security -> Browser Privacy and choose "Custom" and uncheck the "Cookies" option.

0

Changing base URL from localhost to 127.0.0.1. done the trick for me.

0

Login to your cpanel or Access Magento root via FTP open your file php.ini if you already added maxinput_vars increase the number to 10,000 Or Past this at the bottom of file max_input_vars = 10000 Save your file and refresh magento it cache it should work fine !

0

Add: php_value max_input_vars 10000 to file .htaccess, I hope it can help someone!

0

Increasing max_input_vars value in php.ini solved my issue.

0

Make sure you have the corresponding to the URL view/{area}/layout/*.xml file.

This is not directly related to the issue described here (as here we can see on the screenshot the layout is ok), but the error message can be the same.

0

I had the same problem with magento 2.3, where Add to Cart button refreshed the page instead of adding anything in cart. where as website was working properly in Private Browsing on a custom theme.

Solutions Didn't Worked:

  • Change max_input_var=10000
  • max_input_time = 600
  • max_execution_time = 3600
  • Flush cache

Solution Worked for me:

In Configuration >> Web, I:

I am not sure if its a reliable fix or a temporary, but it resolved the issue for now.

0

The problem is with your local virtualhost name. Name should be in 2 parts, separated by a Dot(.) You may be running it like http://localhost/ or http://example/ . It should be split by Dot(.). Dot is necessary otherwise that problem will occur. If you changed my local virtual host name to http://localhost.co/ OR http://example.ty/ then the issue will get fixed.

0

In our case, we missed to update the (all exising baseurl) from the core_config_data, due to this our frontend was working and backend not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.