This is for a school project and I don’t have any experience with XSRF tokens. I attempted to add an XRSF token after reading up online because Jupyter notebook was saying it was missing. Now that it’s added it’s saying the XSRF cookie doesn’t match POST argument. I’m not sure what I’m doing wrong I’ve tried multiple different ways after researching and nothing has worked, any help would really be appreciated.
This is the code I currently have that’s giving me this error, you can see the hidden input type token in the HTML code and then the PHP code below. I’ve also already read other posts that just needed to clear cache/cookies, restart the session, or remove specific XRSF mentions from cookies and I have tried all of the above with no success, I even switched browsers from chrome to edge to see if that worked and it did not.
<form action="user_input.php" method="POST">
<div style="background-color:tan; border:solid black; width:900px; padding:50px;">
<!-- Other form sections -->
<br>
<input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
<input type="submit" value="Submit"/>
</div>
</form>
<?php
session_start();
function generateCSRFToken() {
// Generate random token
$token = bin2hex(random_bytes(32));
$_SESSION['csrf_token'] = $token;
return $token;`
}
// Check if the CSRF token is present and valid
if(isset($_POST['csrf_token']) && $_POST['csrf_token'] === $_SESSION['csrf_token']) {
//handle inputs
}
?>