【问题标题】:Adding captcha to a form is not working correct向表单添加验证码无法正常工作
【发布时间】:2014-12-11 01:37:59
【问题描述】:

我尝试在此表单中添加验证码,如果您愿意,请检查链接。

This is the link from Stackoverflow

但是当我输入验证码错误时,我无法使其正常工作,没有任何反应,我仍然收到邮件。

<?php
session_start();
$cap = 'notEq';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if ($_POST['captcha'] == $_SESSION['cap_code']) {
        // Captcha verification is Correct. Do something here!
        $cap = 'Eq';
    } else {
        // Captcha verification is wrong. Take other action
        $cap = '';
    }



    $to = "exmaple@mail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $ip = $_POST['ip'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . " " . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.

}

?>

下面是验证码和提交按钮。

    <!-- Captcha -->
<input type="text" name="captcha" id="captcha" maxlength="6" size="6"/>


    <!-- Submit Button -->
<input type="submit" id="captcha" name="captcha" value="Submit">

那我在这里做错了什么?

感谢您的帮助。

【问题讨论】:

  • 您在哪里运行设置验证码值并将其存储在 $_SESSION['cap_code'] 上的代码
  • @RiggsFolly 脚本正在运行,直到我从 ($to = "example@mail.com") 到 (end php tag ?>)
  • 你可以使用谷歌recaptcha而不是尝试创建自己的?它会让你更简单,不同的捕获图像的数量将比你个人制作的要多得多google.com/recaptcha/intro

标签: php forms captcha


【解决方案1】:

您的两个输入字段具有相同的名称,因此文本字段的内容很可能被提交按钮的值覆盖。

更改提交按钮的名称。

<input type="text" name="captcha" id="captcha" maxlength="6" size="6"/>

<!-- Submit Button -->
<input type="submit" id="captchaBut" name="captchaBut" value="Submit">

【讨论】:

    猜你喜欢
    • 2012-11-18
    • 2012-04-22
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    相关资源
    最近更新 更多