简体   繁体   English

验证码验证和发送电子邮件

[英]Captcha Validation and Send Email

I have an issue with the validating and further processing Captcha on my form. 我在表单上验证和进一步处理验证码时遇到问题。 I would like to confirm the answer is correct then send out the email and send the user to the thank you page otherwise take it to the error page if the user failed to input the proper response to the Captcha. 我想确认答案是正确的,然后发送电子邮件并将用户发送到“谢谢”页面,否则,如果用户未能对验证码输入正确的答案,请转到错误页面。 I am capturing the captcha code on a hidden input field on the form. 我在窗体的隐藏输入字段上捕获了验证码。 Even after putting in the correct code, it still redirects me to the error.html page I have set up. 即使输入正确的代码后,它仍然会将我重定向到我设置的error.html页面。

See code below I use the 'captcha_code', where I am trying to obtain the value of true/false. 请参阅下面的代码,我使用“ captcha_code”,试图在其中获取true / false的值。 Once the answer is either true or false based on that it'll continue the IF statement of either sending an email or taking it to the Thank-You page. 一旦答案是对还是错,它将继续发送电子邮件或将其转到“感谢页面”的IF语句。

(If there is any code I am missing to post, please do let me know I'll have that code posted up immediately.) (如果我缺少要发布的代码,请让我知道我会立即发布该代码。)

PHP Code: (Updated Code Post Comments Below) *Still being directed to the Error page even if the captcha is correct. 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");
}
}?>

You are posting the form with post. 您正在使用post发布表单。 So, use $_POST for captcha, not $_GET 因此,对验证码使用$_POST ,而不是$_GET

I have figured out the solution to the problem. 我已经找到解决问题的办法。 I first and foremost updated the IF statement that would mail out and redirect me to correct page. 我首先更新了IF语句,该语句会寄出并将我重定向到正确的页面。 I also made sure to updated my $POST & session_start(). 我还确保更新了$ POST和session_start()。

Here is the PHP Code: 这是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");
    }
    }?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM