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

Dom

A knowledge craver who always strive to be wiser everyday.

Subscribe
Notify of
guest

13 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Hussain Badusha
Hussain Badusha
4 years ago

Hi,
Thanks for the help.
I tried you method but it didn’t solve the issue for me

Actually i’m newbie in magento 2 development.
I’ve been working on magento 2 module.
So i created a admin menu and everything works good.
But when i click on the created menu, it goes and redirects admin dashboard page and display the error

“Invalid security or form key. Please refresh the page.”

Fyi, i doing this in windows 7 PC and localhost server xampp

Any help would be appreciable.
Thanks,
Hussain.

Cal
Cal
4 years ago

Hey, I had the same problem, your admin menu action attribute is wrong. So for example, in your app/code/Mageplaza/HelloWorld/etc/adminhtml/menu.xml There is line where you specify your route. You need to change your action=”mageplaza_helloworld/post” It goes like this : action=”{router_name} / {controller_folder} / {action_name}” Your router name is your namespace and module name – in this case it is Mageplaza_HelloWorld Your controller folder is the folder inside app/code/Mageplaza/HelloWorld/Controller/Adminhtml – in your case it may be Post Your action name is the .php file inside app/code/Mageplaza/HelloWorld/Controller/Adminhtml/Post – in your case it may be Index.php TLDR: In your app/code/Mageplaza/HelloWorld/etc/adminhtml/menu.xml On the line that… Read more »

Schoayb
Schoayb
3 years ago

thanks for the help but it dosent work.
I have Magento 2.4.1, max_input_vars is 20000 but still having the invalid key issue for only one product attribute(Dropdown with 13 options), when I save it without change, or update its values. I can edit this particular attribute only…

I made a very similar attribute with a new name and it gets saved without any issue, but why not this original one!

please guide.

Last edited 3 years ago by Schoayb
andrew
1 year ago

Security breaches, performance issues, scalability issues, lack of features and functionality, etc. are the critical issues in Magento. You must fix these issues before it harms your business. https://www.alakmalak.com/?UTM-andrew-aug/ has helped me multiple times to get through this issue efficiently.

jyoti
jyoti
5 years ago

Thank you.You saved my time

Sebastian
Sebastian
4 years ago

Thanks so much – you saved my day!

Mansi
Mansi
4 years ago
Reply to  Dom

I changed max_input_vars, again getting same error

Hussain Badusha
Hussain Badusha
4 years ago
Reply to  Mansi

Hi Mansi,
I’m also having the same error.
Did you manage to solve this error?

If so, what have you done to get rid of that issue?
Thanks.

Schoayb
Schoayb
3 years ago

You able to resolve this issue?

nan
nan
4 years ago

Hi,
I am facing this in some browser.

LQTam
LQTam
3 years ago
Reply to  nan

yes, i think the problem may be in browser ( maybe cache, storage). Because i changed browser and it work.