【发布时间】:2018-07-17 01:28:33
【问题描述】:
****更新****
我的代码似乎没有任何问题;因为使用完全相同的代码创建一个新文档完全有效。所以文件可能以某种方式损坏。见my answer below。
我一生都找不到任何相关的问题或问题。 基本上,我有一个通过 Ajax 发送到 php 进程页面的评论表单。验证后,将数据插入 mySQL 数据库。 Ajax 向用户发送信息,主要是错误信息。我需要一种方法来防止在服务器端使用 php 进行多次提交。
我尝试过使用不使用 Ajax 的表单令牌,但不使用它。 $_SESSION 似乎没有延续到表单处理 php 页面。
问题...
$_SESSION['review_form_token'] 根本不保存任何数据。如果我打印错误的会话数据,它根本不会打印任何东西!但是,我有一个使用会话的 php 验证码,它可以毫无问题地打印出来。当我在页面加载时设置会话时,它会按预期将令牌打印到隐藏输入中。我不明白为什么表单令牌没有传递给表单进程。 session_start() 在两个文档中。我试过ob_start() 无济于事。
请帮我调试一下。
这是我的简化设置...
index.php(在顶部):
<?php
session_start();
$reviewForm_token = md5(uniqid(rand(), true));
$_SESSION['review_form_token'] = $reviewForm_token;
?>
index.php - 表单(某处)
<form id="reviewform" method="post" novalidate>
<input type="text" class="form-control form-style name" name="firstname" size="15" maxlength="40" autocomplete="given-name">
<input type="text" class="form-control form-style name" name="lastname" size="15" maxlength="40" autocomplete="family-name">
<textarea class="form-control form-style" name="review" minlength="50" required></textarea>
<input type="text" class="form-control form-style" name="captcha" autocomplete="off" maxlength="6" required>
<img src="https://via.placeholder.com/200x60" id="captcha-review" alt="Review captcha image" width="200" height="60">
<input type="hidden" name="form_token" value="<?php echo $_SESSION['review_form_token']; ?>">
<button type="submit" name="submit" class="btn btn-primary submit float-right font">Send</button>
<button type="reset" class="btn btn-primary reset font">Reset</button>
</form> <!-- End form -->
index.php - Ajax(在文档底部)
$('#reviewform').submit(function(event) {
// Set some variables
var $this = this,
firstnameInput = $('input[name=firstname]', $this),
lastnameInput = $('input[name=lastname]', $this),
nameInput = $('input.name', $this),
ratingInput = $('input[name=StarRating]', $this),
ratingInputChecked = $('input[name=StarRating]:checked', $this),
stars = $('.star-rating', $this),
reviewInput = $('textarea[name=review]', $this),
captchaInput = $('input[name=captcha]', $this),
form_tokenInput = $('input[name=form_token]', $this),
submitButton = $("button.submit", $this);
// Get the form data
// Must relate to the name attribute...
var formData = {
'firstname': firstnameInput.val(),
'lastname': lastnameInput.val(),
'StarRating': ratingInputChecked.val(),
'review': reviewInput.val(),
'captcha': captchaInput.val(),
'form_token': form_tokenInput.val(),
};
// Process the form
$.ajax({
type: 'POST', // Define the type of HTTP verb we want to use (POST for our form)
url: 'formProcess-review.php', // The url where we want to POST
data: formData, // Our data object
dataType: 'json', // What type of data do we expect back from the server
encode: true
})
.done(function(data) {
// Here we will handle errors and validation messages
if (!data.success) {
// Handle errors for doublepost (form_token) ---------------
if (data.errors.doublepost) {
$('button', $this).parents('.form-row')
.append(label + data.errors.doublepost + '</label>')
.children('label.invalid').attr('id', 'doublepost-error');
}
} else {
// SUCCESS!!
// Thanks!
}
}); // End .done function
// Stop the form from submitting the normal way and refreshing the page
event.preventDefault();
}); // End .submit function
formProcess-review.php
<?php
session_start();
$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$StarRating = $_POST['StarRating'];
$review = $_POST['review'];
$captcha = $_POST['captcha'];
// Form Validation...
if ($_POST['form_token'] !== $_SESSION['review_form_token']) {
$errors['doublepost'] = 'You have already submitted your review, you can not resubmit. If you need to send another review, please reload the page.';
}
// return a response ===========================================================
// if there are any errors in our errors array, return a success boolean of false
if (!empty($errors)) {
// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors'] = $errors;
} else {
// if there are no errors process our form, then return a message
unset($_SESSION['review_form_token']);
// mySQL inserting data...
// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
}
// return all our data to an AJAX call
echo json_encode($data);
?>
【问题讨论】:
-
您是否尝试过在 formProcess-review 中的 session_start 之后使用调试器或转储 $_SESSION?
-
将
else{}添加到if ($_POST['form_token'] !== $_SESSION['review_form_token']) {...}时会发生什么?或者你已经有一个了?使用php的报错,看源码和开发者控制台。 -
@Devon 使用 foreach 循环我可以转储可用会话,其中令牌不属于其中。只有验证码会话被转储。
-
@FunkFortyNiner 老实说,这不是正确的很多,仍然无法正常工作。并且错误报告也没有显示任何内容。我是否做得正确是另一回事,我以这个答案为指导:stackoverflow.com/a/21429652/2358222
-
所有可用会话是什么意思,为什么要使用 foreach 循环?你就不能
var_dump($_SESSION)吗?
标签: php jquery ajax forms session-variables