在PHP表单上的Captcha,只要有人填写此表单,就会显示验证码不正确的错误

时间:2014-09-19 15:51:15

标签: php forms validation

这是我的表格,因为它将电子邮件发送到不同单选按钮的selectoin上的四个不同的电子邮件.....但我不希望提交表单直到验证验证码。所以我添加了这个验证码但是它不起作用我不知道为什么。请帮助我...。提前谢谢。

                <?php
                session_start();
                //error_reporting(E_ERROR | E_PARSE);

                //echo "<pre>";
                //print_r($_REQUEST);
                if(!empty($_POST['answer']) )
                {
                    //echo "You entered " . htmlentities($_POST['answer']) . " which is ";

                    if ($_SESSION['answer'] == $_POST['answer'])
                    {
                        //echo 'correct';
                        $validatedCaptcha = true;
                    }
                    else
                    {
                        echo '<center>'.'You Filled wrong Captcha. We expected ' . $_SESSION['answer'].'      .Kindly Fill the Form Again'.'</center>';
                        $validatedCaptcha = false;
                    }
                }

                $digit1 = mt_rand(1, 20);
                $digit2 = mt_rand(1, 20);
                if (mt_rand(0, 1) === 1) {
                    $math = "$digit1 + $digit2";
                    $_SESSION['answer'] = $digit1 + $digit2;
                } else {
                    if($digit1 >= $digit2)
                    {
                        $math = "$digit1 - $digit2";    
                        $_SESSION['answer'] = $digit1 - $digit2;
                    }
                    else if($digit1 < $digit2)
                    {
                        $math = "$digit2 - $digit1";    
                        $_SESSION['answer'] = $digit2 - $digit1;
                    }
                }
                //echo "<pre>";
                //print_r($_SESSION);
                ?>
                <center>
                    <?php
                    $name = $_REQUEST['name11'];
                    $email = $_REQUEST['email11'];
                    $phone12 = $_REQUEST['mobile11'];
                    $subject = $_REQUEST['subject11'];
                    $message1 = $_REQUEST['message11'];
                    $zone1 = $_REQUEST['zone1'];
                    $null_virdi = "-f " . $email;


                    if (isset($_REQUEST['submit']) && $validatedCaptcha == true ) {

                        $message = "Name: " . $name . "\n" . "Email: " . $email . "\n" . "phone :" . $phone12 . "\n" . "Message :" . $message1;

                        if ($_REQUEST['zone1'] == "South") {

                            echo "Thank you for Contacting our South Indian Zonal Office";
                            mail("abc@gmail.com", $subject, $message, null, $null_virdi);
                        }

                        if ($_REQUEST['zone1'] == "Delhi") {

                            mail("abc@gmail.com", $subject, $message, null, $null_virdi);
                            echo "Thank you for Contacting our Delhi Office";
                        }

                        if ($_REQUEST['zone1'] == "Hyderabad") {

                            mail("abc3@gmail.com", $subject, $message, null, $null_virdi);
                            echo "Thank you for Contacting our Hyderabad & AP Office";
                        }

                        if ($_REQUEST['zone1'] == "Pune") {

                            mail("abc4@gmail.com", $subject, $message, null, $null_virdi);
                            echo "Thank you for Contacting our Pune/Nasik Office";
                        }

                        if ($_REQUEST['zone1'] == "west") {

                            mail("abc5@gmail.com", $subject, $message, null, $null_virdi);
                            echo "Thank you for Contacting our West Zone Office";
                        }
                    }
                    ?>
                    <html><head>
                            <script>
                                function validateForm()
                                {
                                    var x = document.forms["myForm"]["email1"].value;
                                    var atpos = x.indexOf("@");
                                    var dotpos = x.lastIndexOf(".");
                                    if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length)
                                    {
                                        alert("Not a valid e-mail address");
                                        return false;
                                    }
                                }
                            </script>
                        </head><body>

                           <div style="width:50%;border: 4px double #dbd395;margin: 0;padding: 28px 0px 0px 34px;background-color: #F8F8F8">   
                 <form name="myForm" action="" onSubmit="return validateForm();" method="post" >

                             <table >
                                    <tr>
                                    <h3 style="margin-left:5px;">Enquiry for Quotation</h3>
                                    </tr>
                                    <tr>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td>Name&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;<input type="text" required="required" name="name11"  size="40"/></td>
                                    </tr>
                                    <td></td>
                                    </tr>
                                    <tr>
                                        <td>Email&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;<input type="text" required="required" name="email11" size="40"/></td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td>Mobile&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;<input type="text" required="required" name="mobile11" size="40"/></td>
                                    </tr>
                                    <tr>
                                        <td>Zone&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<input type="radio" required="required" name="zone1" value="South"/>South India &nbsp;&nbsp;<input type="radio" name="zone1" value="Delhi" />Delhi NCR <input type="radio" name="zone1" value="Hyderabad"/>Hyderabad & AP <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="zone1" value="Pune"/>Pune/Nashik <input type="radio" name="zone1" value="west"/>Other </td>
                                    </tr>
                                    <td></td>
                                    </tr>
                                    <tr>
                                        <td>Subject&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;<input type="text" required="required" name="subject11" size="40"/></td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td style="vertical-align: top;"><font style=vertical-align:top; color="#000000">Message</font><font style=vertical-align:top; color="#FF0000">* :</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<textarea rows="4" cols="32" name="message11" required="required"/></textarea></td>
                                    </tr>
                <tr><td><center><b>Captcha</b></center></td></tr>
                                    <tr><td align="center">What's <?php echo $math; ?> = <input size="5" name="answer" type="text" /><br /></td></tr>

                                    <tr>
                                        <td align="center">


                                            <input  type="submit" name="submit" value="Send Message"/></td>
                                    </tr>

                                </table>

                            </form>
                </div>
                        <body/>
                    </html>
                </center>

2 个答案:

答案 0 :(得分:0)

您的&#34;验证码&#34;完全是弱者:&#34;答案&#34;是(1 + 1)和(20 + 20)之间的值,在连续行(2 ... 40)中给出不超过39个不同结果的数字。

因为那里有解析器(而且我已经看到他们使用&#34; captchas和#34这样的各种形式工作),这只是简单地从1 ... 100对照表格强制发布随机数像这样,他们需要几分钟的时间来发布/注册/注册到您的网站。

答案 1 :(得分:0)

嗯,我没有得到我要求的正确答案......但问题是我在wordpress页面中使用此表单并且它不起作用,因为验证码验证总是出错并且表格始终用于回应您的验证码是else部分中存在错误。但经过一些研究后我发现问题是由于我在我的表单中使用的会话,我必须知道wordpress有自己的会话,所以验证总是会出错。所以我做了这个,我的表格现在正在运作。希望它也会帮助其他人。

        <?php  //error_reporting(E_ERROR | E_PARSE);  ?>
        <?php

        if(!empty($_POST['answer']) )
        {
            //echo "You entered " . htmlentities($_POST['answer']) . " which is ";

            if ($_REQUEST['answer'] == $_POST['answer'])
            {
                //echo 'correct';
                $validatedCaptcha = true;
            }
            else
            {
                echo '<center>'.'You Filled wrong Captcha. We expected ' . $_REQUEST['answer'].'      .Kindly Fill the Form Again'.'</center>';
                $validatedCaptcha = false;
            }
        }

        $digit1 = mt_rand(1, 20);
        $digit2 = mt_rand(1, 20);
        if (mt_rand(0, 1) === 1) {
            $math = "$digit1 + $digit2";
            $_REQUEST['answer'] = $digit1 + $digit2;
        } else {
            if($digit1 >= $digit2)
            {
                $math = "$digit1 - $digit2";    
                $_REQUEST['answer'] = $digit1 - $digit2;
            }
            else if($digit1 < $digit2)
            {
                $math = "$digit2 - $digit1";    
                $_REQUEST['answer'] = $digit2 - $digit1;
            }
        }
        //echo "<pre>";
        //print_r($_SESSION);
        ?>
        <center>
            <?php
            $name = $_REQUEST['name11'];
            $email = $_REQUEST['email11'];
            $phone12 = $_REQUEST['mobile11'];
            $subject = $_REQUEST['subject11'];
            $message1 = $_REQUEST['message11'];
            $zone1 = $_REQUEST['zone1'];
            $null_virdi = "-f " . $email;


            if (isset($_REQUEST['submit']) && $validatedCaptcha == true ) {

                $message = "Name: " . $name . "\n" . "Email: " . $email . "\n" . "phone :" . $phone12 . "\n" . "Message :" . $message1;

                if ($_REQUEST['zone1'] == "South") {

                    echo "Thank you for Contacting our South Indian Zonal Office";
                    mail("blrbranch@sonatech.net", $subject, $message, null, $null_virdi);
                }

                if ($_REQUEST['zone1'] == "Delhi") {

                    mail("sales@sonatech.net", $subject, $message, null, $null_virdi);
                    echo "Thank you for Contacting our Delhi Office";
                }

                if ($_REQUEST['zone1'] == "Hyderabad") {

                    mail("chandel@sonatech.net", $subject, $message, null, $null_virdi);
                    echo "Thank you for Contacting our Hyderabad & AP Office";
                }

                if ($_REQUEST['zone1'] == "Pune") {

                    mail("manjot@hitechwebsolutions.in,pankaj@sonatech.net", $subject, $message, null, $null_virdi);
                    echo "Thank you for Contacting our Pune/Nasik Office";
                }

                if ($_REQUEST['zone1'] == "west") {

                    mail("sales@sonatech.net", $subject, $message, null, $null_virdi);
                    echo "Thank you for Contacting our West Zone Office";
                }
            }
            ?>
            <html><head>
                    <script>
                        function validateForm()
                        {
                            var x = document.forms["myForm"]["email1"].value;
                            var atpos = x.indexOf("@");
                            var dotpos = x.lastIndexOf(".");
                            if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length)
                            {
                                alert("Not a valid e-mail address");
                                return false;
                            }
                        }
                    </script>
                </head><body>

                   <div style="width:50%;border: 4px double #dbd395;margin: 0;padding: 28px 0px 0px 34px;background-color: #F8F8F8">   
         <form name="myForm" action="" onSubmit="return validateForm();" method="post" >

                     <table >
                            <tr>
                            <h3 style="margin-left:5px;">Enquiry for Quotation</h3>
                            </tr>
                            <tr>
                                <td></td>
                            </tr>
                            <tr>
                                <td>Name&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;<input type="text" required="required" name="name11"  size="40"/></td>
                            </tr>
                            <td></td>
                            </tr>
                            <tr>
                                <td>Email&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;<input type="text" required="required" name="email11" size="40"/></td>
                            </tr>
                            <tr>
                                <td></td>
                            </tr>
                            <tr>
                                <td>Mobile&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;<input type="text" required="required" name="mobile11" size="40"/></td>
                            </tr>
                            <tr>
                                <td>Zone&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<input type="radio" required="required" name="zone1" value="South"/>South India &nbsp;&nbsp;<input type="radio" name="zone1" value="Delhi" />Delhi NCR <input type="radio" name="zone1" value="Hyderabad"/>Hyderabad & AP <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="zone1" value="Pune"/>Pune/Nashik <input type="radio" name="zone1" value="west"/>Other </td>
                            </tr>
                            <td></td>
                            </tr>
                            <tr>
                                <td>Subject&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;<input type="text" required="required" name="subject11" size="40"/></td>
                            </tr>
                            <tr>
                                <td></td>
                            </tr>
                            <tr>
                                <td style="vertical-align: top;"><font style=vertical-align:top; color="#000000">Message</font><font style=vertical-align:top; color="#FF0000">* :</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<textarea rows="4" cols="32" name="message11" required="required"/></textarea></td>
                            </tr>
        <tr><td><center><b>Captcha</b></center></td></tr>
                            <tr><td align="center">What's <?php echo $math; ?> = <input size="5" name="answer" type="text" /><br /></td></tr>

                            <tr>
                                <td align="center">


                                    <input  type="submit" name="submit" value="Send Message"/></td>
                            </tr>

                        </table>

                    </form>
        </div>
                <body/>
            </html>
        </center>