{"id":3001,"date":"2023-11-20T07:14:53","date_gmt":"2023-11-20T07:14:53","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/invalid-form-key-please-refresh-the-page-in-magento-2\/"},"modified":"2025-04-16T09:27:49","modified_gmt":"2025-04-16T03:57:49","slug":"invalid-form-key-please-refresh-the-page-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/invalid-form-key-please-refresh-the-page-in-magento-2\/","title":{"rendered":"Fixed: &#8220;Invalid Form Key. Please Refresh The Page&#8221; in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Facing the Invalid Form Key error in Magento 2? Read on this blog post till the end to find the possible reasons and solutions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Magento 2, a form key (also known as a form nonce) is a security feature to prevent Cross-Site Request Forgery (CSRF) attacks, in which a malicious site or software tries to make unintended requests to the site where the user is authenticated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/add-form-key-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">form key in Magento 2<\/a>&nbsp;ensures that the request is from the end-user and is not a part of any malicious activity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For each session, a unique form key is generated and embedded into the form as an invisible field. And the same is matched with the user\u2019s request to confirm legitimacy of the request.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"magento-invalid-form-key-error-possible-reasons\">Magento 2 Invalid Form Key Error \u2013 Possible Reasons<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In many cases, you may encounter the \u201cInvalid security or form key. Please refresh the page.\u201d error in Magento 2, which may look like:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2023\/11\/Invalid-Form-Key.-Please-Refresh-The-Page-in-Magento-2-Error.jpeg\" alt=\"Invalid Form Key. Please Refresh The Page in Magento 2\" class=\"wp-image-38731\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Here are some common reasons for Magento 2 Invalid Form Key error:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You\u2019re using an outdated version of Magento 2<\/li>\n\n\n\n<li>Incorrect max_input_vars values (too low for Magento 2)<\/li>\n\n\n\n<li>Problems with cookies and sessions due to mismatch of base URL during Migration<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let\u2019s go through the possible ways to solve \u201cInvalid Form Key. Please Refresh The Page\u201d in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-solve-invalid-form-key-please-refresh-the-page-in-magento-\">How to Solve \u201cInvalid Form Key. Please Refresh the Page\u201d in Magento 2?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here are some possible solutions to the Magento 2 Invalid Form Key Error:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Solution 1: Modify the Magento Installtion Directory File<\/li>\n\n\n\n<li>Solution 2: Increase the max_input_vars Value<\/li>\n\n\n\n<li>Solution 3: Change the Magento Base URL<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"solution-modify-the-magento-installtion-directory-file\">Solution 1: Modify the Magento Installtion Directory File<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, back up the&nbsp;<strong><em>root\/vendor\/magento\/module-backend\/App\/Action\/Plugin\/Authentication.php<\/em><\/strong>&nbsp;file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then, open the file and disable the following entities:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$requestParts = explode('\/', trim($request->getRequestUri(), '\/'), 3);\n$baseUrlPath = trim(parse_url($this->backendUrl->getBaseUrl(), PHP_URL_PATH), '\/');\n$routeIndex = empty($baseUrlPath) ? 0 : 1;\n$requestUri = $this->_url->getUrl($requestParts[$routeIndex]);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And add the following entry:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$requestUri = $this->_url->getUrl($this->_url->getStartupPageUrl());<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You\u2019ll get the following output:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">      \/\/ Checks, whether secret key is required for admin access or request uri is explicitly set\n        if ($this->_url->useSecretKey()) {\n#            $requestParts = explode('\/', trim($request->getRequestUri(), '\/'), 3);\n#            $baseUrlPath = trim(parse_url($this->backendUrl->getBaseUrl(), PHP_URL_PATH), '\/');\n#            $routeIndex = empty($baseUrlPath) ? 0 : 1;\n#            $requestUri = $this->_url->getUrl($requestParts[$routeIndex]);\n            $requestUri = $this->_url->getUrl($this->_url->getStartupPageUrl());\n        } elseif ($request) {\n            $requestUri = $request->getRequestUri();\n        }\n\n        if (!$requestUri) {\n            return false;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, check if the Magento 2 Invalid Form Key error reproduces or not. If it does, try the next solution.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"solution-increase-the-maxinputvars-value\">Solution 2: Increase the max_input_vars Value<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One possible reason for the Magento 2 \u201cInvalid security or form key. Please refresh the page.\u201d error can be an incorrect&nbsp;<code>max_input_vars<\/code>&nbsp;value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For the php.ini or .user.ini file, use:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">max_input_vars = 10000<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For the .htaccess file, use this code<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">php_value max_input_vars 10000<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, check if the Magento 2 Invalid Form Key error reproduces or not. If it does, try the next solution.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"solution-change-the-magento-base-url\">Solution 3: Change the Magento Base URL<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The next solution is to change the base URL of Magento. You can do it in two ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using phpMyAdmin<\/li>\n\n\n\n<li>Using CLI<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Here\u2019s how to change Magento 2 Base URL using phpMyAdmin:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Log into phpMyAdmin and find the&nbsp;<strong>core_config_data<\/strong>&nbsp;table in the database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, change the&nbsp;<strong>web\/unsecure\/base_url<\/strong>&nbsp;value to&nbsp;<code>https:\/\/127.0.0.1<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Click \u201c<strong>Go<\/strong>.\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(Make sure to use the correct protocol as per your site i.e. http:\/\/ or https:\/\/)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Here\u2019s how to change Magento 2 Base URL using CLI:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the following command to change the Magento 2 Base URL:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">php bin\/magento setup:store-config:set --base-url=\"http:\/\/127.0.0.1:8080\/\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Make sure to change the protocol asper your site i.e. http:\/\/ or https:\/\/<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, run the following command to flush the cache:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">php bin\/magento cache:flush<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And remove the cache folder by using the following command in the root Magento 2 directory:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">rm -rf var\/cache var\/generation var\/page_cache<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In case you\u2019re still experiencing the Magento 2 Invalid Form Key error, it\u2019s time to consult an expert.\u00a0You can <a href=\"https:\/\/meetanshi.com\/contacts\" target=\"_blank\" rel=\"noreferrer noopener\">contact us now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Facing the Invalid Form Key error in Magento 2? Read on this blog post till the end to find the possible reasons and solutions. In&#8230;<\/p>\n","protected":false},"author":51,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-3001","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/3001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/51"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=3001"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/3001\/revisions"}],"predecessor-version":[{"id":12187,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/3001\/revisions\/12187"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=3001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=3001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=3001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}