验证码和发送电子邮件

时间:2015-07-26 16:13:27

标签: php

我的表单上有验证和进一步处理验证码的问题。我想确认答案是否正确然后发送电子邮件并将用户发送到感谢页面,否则如果用户未能向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");
}
}?>

2 个答案:

答案 0 :(得分:2)

您正在发布带有帖子的表单。因此,请使用$_POST表示验证码,而不是$_GET

答案 1 :(得分:0)

我已经找到了问题的解决方案。我首先更新了将邮寄出来并将我重定向到正确页面的IF语句。我还确保更新我的$ POST&amp;在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");
    }
    }?>