我在表单上验证和进一步处理验证码时遇到问题。我想确认答案是正确的,然后发出电子邮件,并将用户发送到感谢页面,否则将其带到错误页面,如果用户未能输入正确的回应验证码。我在表单的隐藏输入字段上捕获captcha代码。即使在输入正确的代码后,它仍然会将我重定向到我设置的error.html页面。
参见下面的代码我使用了'captcha_ code ',在这里我试图获得true/false的值。一旦答案是真或假,它将继续IF语句,要么发送一封电子邮件,要么将其带到谢谢页面。
(如果我没有发布任何代码,请务必让我知道我会立即发布该代码。)
PHP代码:(更新了下面的代码帖子注释)*即使验证码是正确的,仍然会被定向到错误页面。
<?php
if ($_POST["submit"]) {
$companyName = $_POST['tscpCompanyName'];
$businessType = $_POST['tscpBusinessType'];
$yearsBusiness = $_POST['tscpYears'];
$numberOfUnits = $_POST['tscpNumberOfUnits'];
$firstName = $_POST['tscpFirstName'];
$lastName = $_POST['tscpLastName'];
$email = $_POST['tscpEmail'];
$number = $_POST['tscpNumber'];
$vals = $_POST['vals'];
$human = intval($_POST['captcha_code']);
$from = "From:test@from.com";
$to = "test@to.com";
$subject = 'Custom Package Request';
$body ="Company Name: $companyName\n\n Business Type: $businessType\n\n Years In Business: $yearsBusiness\n\n First Name: $firstName\n\n Last Name: $lastName\n\n Email: $email\n\n Number: $number\n\n Services: $vals\n\n";
if (!isset($_SESSION)) session_start();
if ($_POST['captcha_code'] == $_SESSION['code']) {
echo 'true';
} else {
echo 'false';
}
//Check if simple anti-bot test is correct
if ($human !== false) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
header("Location: /thank-you/mortgage-lending.html");
} else {
header("Location: /error.html");
}
}
else {
header("Location: /error.html");
}
}?>发布于 2015-07-27 00:22:32
您正在使用post发布表单。因此,对验证码使用$_POST,而不是$_GET
发布于 2015-07-27 10:18:49
我已经想出了解决这个问题的办法。我首先更新了IF语句,该语句将邮寄出去,并将我重定向到正确的页面。我还确保更新了我的$POST & session_start()。
以下是PHP代码:
<?php
if (!isset($_SESSION)) session_start();
if ($_POST["submit"])
{
$companyName = $_POST['tscpCompanyName'];
$businessType = $_POST['tscpBusinessType'];
$yearsBusiness = $_POST['tscpYears'];
$numberOfUnits = $_POST['tscpNumberOfUnits'];
$firstName = $_POST['tscpFirstName'];
$lastName = $_POST['tscpLastName'];
$email = $_POST['tscpEmail'];
$number = $_POST['tscpNumber'];
$vals = $_POST['vals'];
$human = intval($_POST['captcha_code']);
$captchaError= FALSE;
$from = "From:test@from.com";
$to = "test@test.com";
$subject = 'Custom Package Request';
$body ="Company Name: $companyName\n\n Business Type: $businessType\n\n Years In Business: $yearsBusiness\n\n First Name: $firstName\n\n Last Name: $lastName\n\n Email: $email\n\n Number: $number\n\n Services: $vals\n\n";
if ($_POST['captcha_code'] != $_SESSION['code'])
{
$captchaError = TRUE;
$errHuman = 'Your anti-spam is incorrect';
}
if (!$errName && !$errEmail && !$errMessage && !$captchaError) {
if (mail ($to, $subject, $body, $from)) {
header("Location: /thank-you/mortgage-lending.html");
} else {
header("Location: /error.html");
}
}
else {
header("Location: /error.html");
}
}?>https://stackoverflow.com/questions/31639139
复制相似问题