我需要帮助来发送带有ajax请求的令牌。他总是说:“无效”。
我犯了什么错误?
world.php:
<form name="theform" id="suchform" method="POST">
<p><input type="text" class="find_person search_btn" placeholder="Person Suchen" tabindex="1"></p>
<p><input type="hidden" class="csrf_token" value="<?= echo $_SESSION['csrf_token']; ?>"></p>
</form>
<div id="output">
</div>
<script>
$('#suchform').on('input', function(event) {
event.preventDefault();
var name = $('#suchform').find('.find_person').val();
var token = $('#suchform').find('.csrf_token').val();
$.ajax({
type: 'POST',
url: 'show_user.php',
data: {find_person:name, csrf_token:token},
success: function(data) {
$('#output').html(data);
}
})
})
</script>
show_user.php
if($_POST['csrf_token'] !== $_SESSION['csrf_token']) {
echo $_POST['csrf_token'] . '<br>' . $_SESSION['csrf_token']. '<BR>';
exit("invalid");
}
connect.php
$_SESSION['csrf_token'] = bin2hex(random_bytes(16));