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?
<?php
function csrf_token() {
return bin2hex(random_bytes(35));
}
function create_csrf_token() {
$token = csrf_token();
$_SESSION['csrf_token'] = $token;
$_SESSION['csrf_token_time'] = time();
return $token;
}
function csrf_token_tag() {
$token = create_csrf_token();
return '<input type="hidden" name="csrf_token" value="' . $token . '">';
}
$csrf_token = csrf_token_tag();
?>
<form action="" method="post">
...
<?= $csrf_token; ?>
</form>
<form action="" method="post">
...
<?= $csrf_token; ?>
</form>