{"id":23771,"date":"2024-04-05T12:27:36","date_gmt":"2024-04-05T12:27:36","guid":{"rendered":"https:\/\/ecommerce.folio3.com\/blog\/?p=23771"},"modified":"2025-08-28T16:13:04","modified_gmt":"2025-08-28T16:13:04","slug":"how-to-fix-invalid-form-key-please-refresh-the-page-error-in-magento-2","status":"publish","type":"post","link":"https:\/\/ecommerce.folio3.com\/blog\/how-to-fix-invalid-form-key-please-refresh-the-page-error-in-magento-2\/","title":{"rendered":"How to fix \u201cInvalid form key. Please refresh the page.\u201d error in Magento 2"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">The &#8220;Invalid form key. Please refresh the page.&#8221; error in Magento 2 typically occurs due to issues when there&#8217;s a mismatch between the form key stored in the session and the form key submitted with a form request. Form keys are a security feature in Magento that assists with Cross-Site Request Forgery (CSRF) attacks by ensuring that form submissions originate from the same Magento instance.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For every session, a one-of-a-kind unique form key is produced and inserted into the form as an invisible field. Also, the equivalent is coordinated with the client&#8217;s request to affirm the legitimacy of the request.<\/span><\/p>\n<p style=\"text-align: center;\"><strong>Read Also:<\/strong> <a href=\"https:\/\/ecommerce.folio3.com\/blog\/magento-2-soap-api-guide\/\">Magento 2 SOAP API &#8211; A Comprehensive Guide<\/a><\/p>\n<p><span style=\"font-weight: 400;\">Invalid form key. Please refresh the page. Magento 2 It is one of the most simple errors you&#8217;ve probably encountered. It might appear when you:<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>&#8211;<\/strong> install Magento 2 extensions<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>&#8211;<\/strong> update Magento 2<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>&#8211;<\/strong> create a localhost account\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>&#8211;<\/strong> add multiple child products in configurable product<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>&#8211;<\/strong> add a lot of upsell and cross-sell products<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>&#8211;<\/strong> try to save an attribute with many options<\/span><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-23773 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/1.jpg\" alt=\"Magento 2 Error\" width=\"975\" height=\"183\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/1.jpg 975w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/1-300x56.jpg 300w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/1-768x144.jpg 768w\" sizes=\"(max-width: 975px) 100vw, 975px\" \/><\/p>\n<p><a href=\"https:\/\/ecommerce.folio3.com\/contact-us\/\"><img decoding=\"async\" class=\"alignnone size-full wp-image-28154\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-13.jpg\" alt=\"\" width=\"850\" height=\"160\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-13.jpg 850w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-13-300x56.jpg 300w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-13-768x145.jpg 768w\" sizes=\"(max-width: 850px) 100vw, 850px\" \/><\/a><\/p>\n<h2><b>Three Main Reasons why the Magento 2 \u201cInvalid form key. Please refresh the page.\u201d error appears<\/b><\/h2>\n<h3><b>Server Configuration Issues<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Incorrect server configurations related to sessions, cookies, or security settings can also trigger form key validation errors.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, misconfigured PHP settings Magento platform has specific server requirements for PHP <\/span><b>max_input_vars<\/b><span style=\"font-weight: 400;\"> value. It is the maximum number of variables that the server can utilize for a single request to avoid over-burdening (this value is utilized mainly to safeguard your server from DDOS assaults).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Naturally, it is set to <\/span><b>1000<\/b><span style=\"font-weight: 400;\"> which is inadequate for Magento and causes the error.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Also ensure that your PHP settings are correctly configured, especially session.<\/span><b>cookie_domain<\/b><span style=\"font-weight: 400;\"> and session.<\/span><b>cookie_secure<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h3><b>Base Url Setting<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Likewise, you could get this issue on localhost. It shows up if you use &#8220;<\/span><b>localhost<\/b><span style=\"font-weight: 400;\">&#8221; for the base URL rather than &#8220;<\/span><b>127.0.0.1<\/b><span style=\"font-weight: 400;\">&#8221; and Magento simply doesn&#8217;t recognize it. It causes the cookie misrecognition and correspondingly Magento &#8220;Invalid form key. Please refresh the page.&#8221; error appears.<\/span><\/p>\n<h3><b>Incorrect Form Key<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">You may also get this error on a storefront when you attempt to submit a form. This issue shows up on the off chance that you have an old version of Magento or an old form and not all javascript files are loaded for the mentioned activity to be performed (e.g. add to cart activity).<\/span><\/p>\n<h3><b>Others<\/b><\/h3>\n<h4><b>Session Issues<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Problems with sessions, such as expired or invalidated sessions, can lead to key validation errors. Magento relies on sessions to manage form keys, so any issues with session management can cause this error.<\/span><\/p>\n<h4><b>Caching Problems<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Caching issues, especially if the full page cache (<\/span><b>FPC<\/b><span style=\"font-weight: 400;\">) is enabled, can interfere with form key validation. Cached pages might contain outdated form keys or invalid session data, causing the error to occur.<\/span><\/p>\n<h4><b>Third-Party Extensions or Custom Code<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Sometimes, third-party extensions or custom code can conflict with Magento&#8217;s form key validation logic. If an extension modifies form submission behavior without handling form keys correctly, it can lead to the \u201c<\/span><b>Invalid form key. Please refresh the page.<\/b><span style=\"font-weight: 400;\">\u201d error.<\/span><\/p>\n<p><a href=\"https:\/\/ecommerce.folio3.com\/contact-us\/\"><img decoding=\"async\" class=\"alignnone wp-image-28155 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-14.jpg\" alt=\"\" width=\"2550\" height=\"480\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-14.jpg 2550w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-14-300x56.jpg 300w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-14-1024x193.jpg 1024w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-14-768x145.jpg 768w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-14-1536x289.jpg 1536w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-14-2048x386.jpg 2048w\" sizes=\"(max-width: 2550px) 100vw, 2550px\" \/><\/a><\/p>\n<h2><b>Solutions for the \u201cInvalid form key. Please refresh the page.\u201d error in Magento 2<\/b><\/h2>\n<h3><strong>Solution 1: Increase the max_input_vars in PHP<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">To increase the <\/span><b>max_input_vars<\/b><span style=\"font-weight: 400;\"> variable in PHP, you&#8217;ll need to modify your PHP configuration settings. Increasing it can be necessary when dealing with forms or data submissions that exceed the default limit.\u00a0<\/span><\/p>\n<p><strong>Here&#8217;s how you can do it:<\/strong><b><\/b><\/p>\n<h4><b>&#8211; Locate php.ini File:<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Find the <\/span><b>php.ini<\/b><span style=\"font-weight: 400;\"> file used by <\/span><b>Apache<\/b><span style=\"font-weight: 400;\">. The location of this file can vary depending on your server setup. Common locations include <\/span><b>\/etc\/php\/7.x\/apache2\/php.ini<\/b><span style=\"font-weight: 400;\"> on Linux servers.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Find the <\/span><b>php.ini<\/b><span style=\"font-weight: 400;\"> file used by <\/span><b>PHP-FPM<\/b><span style=\"font-weight: 400;\">. The location can vary but is often found at <\/span><b>\/etc\/php\/7.x\/fpm\/php.ini <\/b><span style=\"font-weight: 400;\">on Linux servers.<\/span><b><\/b><\/p>\n<h4><b>&#8211; Edit php.ini:<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Open the <\/span><b>php.ini<\/b><span style=\"font-weight: 400;\"> file using a text editor (e.g., <\/span><b>nano<\/b><span style=\"font-weight: 400;\">, <\/span><b>vim<\/b><span style=\"font-weight: 400;\">, or <\/span><b>gedit<\/b><span style=\"font-weight: 400;\">).<\/span><b><\/b><\/p>\n<h4><b>&#8211; Find max_input_vars:<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Search for the <\/span><b>max_input_vars<\/b><span style=\"font-weight: 400;\"> variable in the <\/span><b>php.ini<\/b><span style=\"font-weight: 400;\"> file. If it&#8217;s not there, you can add it under the appropriate section (e.g., <\/span><b>[PHP]<\/b><span style=\"font-weight: 400;\"> section).<\/span><br \/>\n<b><\/b><\/p>\n<h4><b>&#8211; Increase max_input_vars:<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Set the value of <\/span><b>max_input_vars<\/b><span style=\"font-weight: 400;\"> to the desired limit. For example:<\/span><\/p>\n<p><b>max_input_vars = 3000<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The suggested value for Magento 2 <\/span><b>max_input_vars<\/b><span style=\"font-weight: 400;\"> is <\/span><b>10000<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h4><b>&#8211; Save and Exit:<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Save the changes to the php.ini file and exit the text editor.<\/span><b><\/b><\/p>\n<h4><b>&#8211; Restart <\/b><b>WebServer<\/b><b>:<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Restart the Apache to apply the changes. You can do this using the command:<\/span><\/p>\n<p><b>sudo service apache2 restart<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Restart PHP-FPM to apply the changes. You can do this using a command like:<\/span><\/p>\n<p><b>sudo service php7.x-fpm restart<\/b><\/p>\n<h3><strong>Solution 2: Change your base URL<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">To change the base URL of your local environment to <\/span><b>127.0.0.1<\/b><span style=\"font-weight: 400;\"> in Magento 2, you need to update the configuration settings in the Magento database. You can do this via the command line or by directly modifying the database.<\/span><\/p>\n<p><strong>Here are the steps:<\/strong><b><\/b><\/p>\n<h4><b>&#8211; Access Your Magento Database:<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Use a database management tool like phpMyAdmin or connect to your database using the command line.<\/span><b><\/b><\/p>\n<h4><b>&#8211; <\/b><b>Find Your Core Configuration Data Table:<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Look for the <\/span><b>core_config_data<\/b><span style=\"font-weight: 400;\"> table in your Magento database.<\/span><b><\/b><\/p>\n<h4><b>&#8211; <\/b><b>Update Base URL Values:<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Identify the rows with the paths <\/span><b>web\/unsecure\/base_url <\/b><span style=\"font-weight: 400;\">and <\/span><b>web\/secure\/base_url<\/b><span style=\"font-weight: 400;\">. These rows contain the base URLs for your Magento store.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Update the value column for both rows to <\/span><b>http:\/\/127.0.0.1\/<\/b><span style=\"font-weight: 400;\"> for unsecure base URL and <\/span><b>https:\/\/127.0.0.1\/<\/b><span style=\"font-weight: 400;\"> for the secure base URL (if you&#8217;re using HTTPS in your local environment).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You can run SQL queries like:<\/span><\/p>\n<p><b>UPDATE core_config_data SET value = &#8216;http:\/\/127.0.0.1\/&#8217; WHERE path = &#8216;web\/unsecure\/base_url&#8217;;<\/b><\/p>\n<p><b>UPDATE core_config_data SET value = &#8216;https:\/\/127.0.0.1\/&#8217; WHERE path = &#8216;web\/secure\/base_url&#8217;;<\/b><\/p>\n<p><span style=\"font-weight: 400;\">You can also run the following command from the project root directory to change it:<\/span><\/p>\n<p><b>php bin\/magento setup:store-config:set &#8211;base-url=&#8221;http:\/\/127.0.0.1\/<\/b><\/p>\n<p><b>php bin\/magento setup:store-config:set &#8211;base-url-secure=&#8221;https:\/\/127.0.0.1\/<\/b><b><\/b><\/p>\n<h4><b>&#8211; <\/b><b>Clear Magento Cache:<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Clear the Magento cache to ensure the changes take effect. You can do this via the command line: <\/span><b>php bin\/magento cache:clean<\/b><\/p>\n<h3><strong>Solution 3: Consistently Update Magento<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">If you experience the &#8220;Invalid form key. Please refresh the page.&#8221; error that shows up on a storefront in the latest version of Magento, request a developer investigate it. They need to alter the Add to Cart button layout to make it accessible only after all JS scripts are loaded.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Note: on the off chance that you are utilizing a custom extension or theme, there could be a ton of reasons and correspondingly fixed courses to work on. Check with the theme or custom extension developers.<\/span><\/p>\n<p style=\"text-align: center;\"><strong>Read Also:<\/strong> <a href=\"https:\/\/ecommerce.folio3.com\/blog\/advanced-reporting-and-analytics-in-magento-2-guide\/\" target=\"_blank\" rel=\"noopener\">Ultimate Guide To Advanced Reporting And Analytics In Magento 2<\/a><\/p>\n<h2><b>Conclusion<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">In conclusion, the &#8220;Invalid form key. Please refresh the page.&#8221; error in Magento 2 is a common issue that occurs due to various reasons such as expired form keys, session issues, incorrect server configurations, caching problems, browser settings, or conflicts with third-party extensions or custom code.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We trust that you can solve the issue yourself by adhering to the step-by-step guidelines above.<\/span><\/p>\n<h2><b>FAQ&#8217;s<\/b><\/h2>\n<h3><b>Q: What is a form key?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">A form key (also known as a CSRF token) is a randomly generated value that Magento embeds in forms to verify that the form submission originated from the same site and wasn&#8217;t forged by a malicious third party.<\/span><\/p>\n<h3><b>Q: Why does the \u201cInvalid form key. Please refresh the page.\u201d error occur?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">The error occurs when Magento receives a form submission without a valid or matching form key. This can happen due to various reasons such as expired sessions, browser issues, caching problems, incorrect server configurations, or custom code conflicts.<\/span><\/p>\n<p><a href=\"https:\/\/ecommerce.folio3.com\/contact-us\/\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-28156 size-full\" src=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-15.jpg\" alt=\"\" width=\"2550\" height=\"480\" srcset=\"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-15.jpg 2550w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-15-300x56.jpg 300w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-15-1024x193.jpg 1024w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-15-768x145.jpg 768w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-15-1536x289.jpg 1536w, https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/Worried-about-breaking-your-store-during-migration_-15-2048x386.jpg 2048w\" sizes=\"(max-width: 2550px) 100vw, 2550px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;Invalid form key. Please refresh the page.&#8221; error in Magento 2 typically occurs due to issues when there&#8217;s a mismatch between the form key stored in the session and the form key submitted with a form request. Form keys are a security feature in Magento that assists with Cross-Site Request Forgery (CSRF) attacks by<\/p>\n","protected":false},"author":45,"featured_media":23775,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[27],"tags":[297,298],"class_list":{"0":"post-23771","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-magento","8":"tag-fix-magento-2-error","9":"tag-invalid-form-key-please-refresh-the-page"},"acf":[],"featured_image_data":{"src":"https:\/\/ecommerce.folio3.com\/blog\/wp-content\/uploads\/2024\/04\/9170819.jpg","alt":"magento error","caption":""},"_links":{"self":[{"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/posts\/23771"}],"collection":[{"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/comments?post=23771"}],"version-history":[{"count":0,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/posts\/23771\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/media\/23775"}],"wp:attachment":[{"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/media?parent=23771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/categories?post=23771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ecommerce.folio3.com\/blog\/wp-json\/wp\/v2\/tags?post=23771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}