我的形式简单的反机器人过滤器

时间:2014-01-07 23:04:40

标签: php

我为电子邮件表单制作了这个小CAPTCHA类型的东西,但出于某种原因,即使我输入了正确的答案(在这种情况下为4),我也得到了else语句。为什么呢?

以下是代码:

HTML

<div class="form-group col-lg-3">
<label>What is the sum of 2 and 2?</label>
    <input type="text" class="form-control" name="robo_q" id="robo_q">
</div>

PHP

<?php
    if ($_POST['$robo_q'] == 4) {

        $nambre = $_POST['name'];
        $number = $_POST['number'];
        $email = $_POST['email'];
        $user_message = $_POST['message'];
        $message = "<h3>From:&nbsp;".$nambre."</h1>";
        $message .= "<h3>Phone:&nbsp;".$number."</h3><br/>";
        $message .=$user_message;

        $headers = "From: " . strip_tags($_POST['email']) . "\r\n";
        $headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        mail ('email@email.com', 'Someone is contacting you from you website', $message, $headers );
        header("location: success.html");
    }
    else {

      header("location: recaptcha.html");
    }
?>

3 个答案:

答案 0 :(得分:5)

此:

if ($_POST['$robo_q'] == 4) {

应该是:

if ($_POST['robo_q'] == 4) {

请注意删除$。 $ _POST 必须中的键与您在HTML表单中嵌入的name完全匹配。

答案 1 :(得分:3)

你正在寻找错误的变量。

试试这个:

if ($_POST['robo_q'] == 4) {   // No $

答案 2 :(得分:1)

问题出在这里

if ($_POST['$robo_q'] == 4) {

$robo_q不是变量,只需将其更改为robo_q。您的新代码将是

<?php

    if ($_POST['robo_q'] == 4) {

        $nambre = $_POST['name'];
        $number = $_POST['number'];
        $email = $_POST['email'];
        $user_message = $_POST['message'];
        $message = "<h3>From:&nbsp;".$nambre."</h1>";
        $message .= "<h3>Phone:&nbsp;".$number."</h3><br/>";
        $message .=$user_message;

        $headers = "From: " . strip_tags($_POST['email']) . "\r\n";
        $headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        mail ('email@email.com', 'Someone is contacting you from you website', $message, $headers);
        header("location: success.html");
    }
    else {

      header("location: recaptcha.html");
    }
?>