Skip to content
Advertisement

PHP – How to use csrf token in multiple forms on the same page

I have 2 forms on the same page. I want to use the csrf token in both forms. when I try to use it, it regenerates the csrf token on form submission.

How can I solve this?

JavaScript

Advertisement

Answer

When you refresh the page, the function created_csrf_token gets fired again, changing the CSRF token.

As Lawrence has commented, you can scope it. E.g.

JavaScript

Adding an isset into the create_csrf_token will check if the CSRF token is set, and return the existing, rather than create a new one.

You probably want to add some logic though, to create a new CSRF token after around 5 minutes.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement