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


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.


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 “”:

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

Or go to your PHPAdmin and find the following table: core_config_data, and change the value of web/unsecure/base_url to

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

Is it helpful?

A knowledge craver who always strive to be wiser everyday.

Notify of
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Hussain Badusha
Hussain Badusha
1 year ago

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.

1 year 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 »

7 months 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 7 months ago by Schoayb
3 years ago

Thank you.You saved my time

2 years ago

Thanks so much – you saved my day!

1 year ago
Reply to  Dom

I changed max_input_vars, again getting same error

Hussain Badusha
Hussain Badusha
1 year 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?

7 months ago

You able to resolve this issue?

1 year ago

I am facing this in some browser.

8 months ago
Reply to  nan

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