Hello Magento Friends,

In this blog, we will learn How to Add Form Key in phtml File in Magento 2.

Form key helps to prevent Cross-Site Request Forgery attack. This means an attacker encourages users to perform actions they don’t intend to. Using the form key in Magento 2, you can keep your site safe from CSRF (Cross-Site Request Forgery) attacks.

Let’s learn how to add a form key in Magento 2.

Steps to Add Form Key in phtml File in Magento 2:

Step 1: Create block file Index.php in the given path

app/code/Vendor/Extension/Block/Index/Index.php

Now add the code as given below

Step 2: Create layout file customer_index_index.xml at the below path

app/code/Vendor/Extension/view/frontend/layout/ customer_index_index.xml

And embed the below code

Step 3: Create a customer.phtml file at the following path

app/code/Vendor/Extension/view/frontend/templates/customer.phtml

Now add the below-mentioned code

The input tag will return with the form as given below. You can use this key based on your requirement.

Conclusion:

Using the above steps, you can easily add form key in phtml file in Magento 2. To add advanced security to your Magento 2 store, avail of Magento Security Patches Installation Service and safeguard your store from vulnerabilities.

Share the article with other Magento merchants, and stay with us!

Happy Coding!

Click to rate this post!
[Total: 2 Average: 5]