cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 How to save "Invalid Form Key" error to a log file

Magento 2 How to save "Invalid Form Key" error to a log file

Hello,

 

Is it possible to capture and save the "Invalid Form Key" error to a log file? This way we could learn when it occurred and see what processes were running at that time.

 

Regards

4 REPLIES 4

Re: Magento 2 How to save "Invalid Form Key" error to a log file

Magento 2 does not provide a built-in log file for the "Invalid Form Key" error. However, you can create a custom module to log this error to a log file.

 

First, create a custom module and add a new observer for the event "controller_action_predispatch_checkout_index_index." Check for the "Invalid Form Key" error in the observer's execute method. If the error is encountered, log in to the log file.

 

Finally, add a regular cron job to check the log file for any "Invalid Form Key" errors and take the necessary action.

 

This way, you can easily save the "Invalid Form Key" error to a log file.

 

Or increase max_input_vars to 1000 or more.

 

To increase the max_input_vars to 1000 or more, you need to edit your php.ini file and set the value of max_input_vars to 1000 or more. You can find the php.ini file at the root of your server.

 

Once you edit the php.ini file, you need to restart the web server for the changes to take effect.

 

Once the changes are implemented, your server should be able to handle more input variables and help you prevent the "Invalid Form Key" error.

 

Or change your base URL

 

To change your base URL, you need to edit the configuration settings in the Magento Admin Panel.

 

Navigate to Stores > Configuration > General > Web.

 

Under Base URLs, make sure that the Base URLs and Base URLs (Secure) are set to your desired URL.

 

Once you save the settings, your base URL will be changed, and you should no longer encounter the "Invalid Form Key" error.

Re: Magento 2 How to save "Invalid Form Key" error to a log file

Hello,

 

We already tried the other 2 solutions you have mentioned and we still encountered this. We will try the solution you mentioned about creating a custom module for controller_action_predispatch_checkout_index_index.

 

Regards

Re: Magento 2 How to save "Invalid Form Key" error to a log file

If you get the dreaded “Invalid form key” error while logging in or working in the admin, something is wrong with your setup. Since version 1.9. 2.2 (and patch SUPEE-6788), Magento requires a secret form token to prevent XSRF attacks.


@nfdsoriano674c Flying Together UAL wrote:

Hello,

 

Is it possible to capture and save the "Invalid Form Key" error to a log file? This way we could learn when it occurred and see what processes were running at that time.

 

Regards


 

Re: Magento 2 How to save "Invalid Form Key" error to a log file

@nfdsoriano674c 

 

Hey,

 

You can create a plugin for validate() of Magento\Framework\Data\Form\FormKey\Validator and log the request from which action it comes.

 

Thank You!

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.