3

我已经成功地使用 php 创建了一个联系表单,它提供了各种所需的消息。现在我想添加一个简单的随机算术验证码(非图像)。请参阅下面的匿名(工作)html 表单和现有(工作,但没有算术验证码)php。

这个想法是以与其他错误消息相同的方式显示“不正确的答案”,或者传递到Thankyou页面以获得正确填写的正确答案的表格。我在这方面做得很好,但不能让它发挥作用。非常感谢任何帮助。


HTML:

    <p>Area of interest:
    <input type="radio" name="likeit" value="A" checked="checked" /> A
    <input type="radio" name="likeit" value="B" /> B
    <input type="radio" name="likeit" value="C" /> C</p>

    <p>How did you hear about us?
    <select name="how">
    <option value=""> -- Please select -- </option>
    <option>Recommendation</option>
    <option>Internet</option>
    <option>Advertisement</option>
    <option>Other</option>
    </select></p>

    <p><strong>Your message subject:</strong><br /><input type="text" name="subject" size="35"/></p>

    <p><strong>Your message:</strong><br />
    <textarea name="comments" rows="10" cols="40"></textarea></p>

    <p>Please answer the following arithmetic question: What is <?php echo $digit1;?> + <?php echo $digit2;?>?
     <input name="captcha" type="text" size="2" id="captcha"/></p>


    <p><input type="submit" value="Send" /></p>

    </form>

PHP:

    <?php
    /* Contact form with arithmetic captcha */
    $myemail  = "enquiries@X.co.uk";

    /* Check all form inputs using check_input function */
    $yourname = check_input($_POST['yourname'], "Enter your name");
    $email    = check_input($_POST['email']);
    $telephone  = check_input($_POST['telephone']);
    $website  = check_input($_POST['website']);
    $likeit   = check_input($_POST['likeit']);
    $how_find = check_input($_POST['how']);
    $subject  = check_input($_POST['subject'], "Add a subject");
    $comments = check_input($_POST['comments'], "Add your message");

    /* If e-mail is not valid show error message */
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
    {
            show_error("Email address is not valid");
    }

    /* If URL is not valid set $website to empty */
    if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
    {
        $website = '';
    }

    /* Message for the email */
    $message = "Hello!

    Your contact form has been submitted by:

    Name: $yourname
    Email: $email
    Telephone: $telephone
    URL: $website

    Area of interest? $likeit
    How did they find us? $how_find

    Comments:
    $comments

    End of message
    ";

    /* Send the message using mail() function */
    mail($myemail, $subject, $message);

    /* Redirect visitor to the thankyou page */
    header('Location: thankyou.html');
    exit();

    /* Functions used */
    function check_input($data, $problem='')
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        if ($problem && strlen($data) == 0)
        {
            show_error($problem);
        }
        return $data;
    }

    function show_error($myError)
    {
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <!--
    Head data in here
    -->
        <html xmlns="http://www.w3.org/1999/xhtml">

        <body>

            <div id="mainheader">

    <div id="mainlogo">

    <h1><a href="http://www.X.co.uk/" title="X">
                                    <img style="border:0;width: 260px; height: 160px;" src="images/X.jpg" alt="X" /></a></h1>
    </div>
    </div>

    <div id="content">
            <div class="content">
                    <h2 class="title">Error!</h2>

    <p><strong>Please correct the following error:</strong></p>
    <p><?php echo $myError; ?></p>

            </div>
    </div>

        <div id="panel">

            <div id="main" class="boxed">
                <h2 class="heading">Main</h2>
                <ul>
                    <li><a href="index.html">Home</a> </li>
                    <li><a href="about.html">About</a> </li>
                    <li><a href="contact.html">Contact</a> </li>
                </ul>
            </div>

            <div id="services" class="boxed">
                <h2 class="heading">Services</h2>
                <ul>
                    <li><a href="services.html">Services</a> </li>
                    <li><a href="recent-projects.html">Recent projects</a> </li>
                </ul>
            </div>

            <div id="pricing" class="boxed">
                <h2 class="heading">Pricing</h2>
                <ul>
                    <li><a href="pricing.html">Pricing</a> </li>
                </ul>
            </div>

            <div id="info" class="boxed">
                <h2 class="heading">Info</h2>
                <ul>
                    <li><a href="tips-and-tricks.html">Tips and tricks</a> </li>
                    <li><a href="useful-links.html">Useful links</a> </li>
                    <li><a href="faq.html">Frequently asked questions</a> </li>
                    <li><a href="site-map.html">Site map</a> </li>
                </ul>
            </div>

                    <div id="contact" class="boxed">
                            <h2 class="heading">Contact</h2>
                            <ul>
                            <li><a href= "mailto:enquiries&#38;#64;X.co.uk">Contact by email</a> </li>
                            <li><strong>Telephone:<br />X</strong> </li>
                            </ul>
                    </div>

        </div>

       <div id="mainfooter">
                    <p> &#38;#169; 2011 X<br />Designed by <a href="http://www.X.co.uk/" title="X"><strong>X</strong></a> </p>
                            <a href="http://validator.w3.org/check?uri=referer" title="Valid XHTML 1.0">
                    <img style="border:0;width:88px;height:31px" src="images/valid-xhtml10.png" alt="Valid XHTML 1.0" />
                </a>
                            <a href="http://jigsaw.w3.org/css-validator/check/referer" title="Valid CSS!">
                    <img style="border:0;width:88px;height:31px" src="images/vcss.gif" alt="Valid CSS!" />
                </a>
        </div>

        </body>
        </html>
    <?php
    exit();
    }
    ?>
4

2 回答 2

12

一般来说,验证码的想法是防止自动处理表单。任何非图像比较都将很容易解决。

无论如何,我会使用会话来解决这个问题。

只需将预期结果存储在第一页的会话变量中,并确保它与第二页匹配

page1.php:

    <?php

    session_start();

    $digit1 = mt_rand(1,20);
    $digit2 = mt_rand(1,20);
    if( mt_rand(0,1) === 1 ) {
            $math = "$digit1 + $digit2";
            $_SESSION['answer'] = $digit1 + $digit2;
    } else {
            $math = "$digit1 - $digit2";
            $_SESSION['answer'] = $digit1 - $digit2;
    }

    ?>

    <form method="POST" action="page2.php">
            What's <?php echo $math; ?> = <input name="answer" type="text" /><br />
            <input type="submit" />
    </form>

page2.php

    session_start();

    echo "You entered ".htmlentities($_POST['answer'])." which is ";

    if ($_SESSION['answer'] == $_POST['answer'] )
            echo 'correct';
    else
            echo 'wrong. We expected '.$_SESSION['answer'];

    ?>
于 2013-03-18T05:58:59.580 回答
0

使用简单的 PHP 数学验证码 https://github.com/kmlpandey77/MathCaptcha

数学验证码

一个简单的 PHP 数学验证码

用法

composer require kmlpandey77/math-captcha

图像中的数学

它将在图像中返回数学

创造captcha.php

<?php
require_once 'vendor/autoload.php'; // link to vendor's autoload.php

use Kmlpandey77\MathCaptcha\Captcha;

$captcha = new Captcha();
$captcha->image();

创造form.php

<form action="check.php" method="post">
    <p>
        Answer it <img src="./captcha.php" alt="" valign="middle">  <input type="text" name="captcha">
    </p>
    <p><button type="submit" name="submit">Submit</button></p>
</form>

文本中的数学

它将以文本形式返回数学

创造form.php

将此代码放在顶部form.php

<?php
require_once 'vendor/autoload.php'; // link to vendor's autoload.php

use Kmlpandey77\MathCaptcha\Captcha;
?>

并将此代码放入body

<form action="check.php" method="post">
    <p>
        Answer it <?php echo new Captcha; ?>  <input type="text" name="captcha">
    </p>
    <p><button type="submit" name="submit">Submit</button></p>
</form>

查看

检查用户是否输入了正确的验证码

创造check.php

<?php
require_once 'vendor/autoload.php'; // link to vendor's autoload.php
use Kmlpandey77\MathCaptcha\Captcha;

if(isset($_POST['submit'])){

    if(Captcha::check()){

        //valid action

        echo('<font color="green">Answer is valid</font>');
    }else{
        echo('<font color="red">Answer is invalid</font>');
    }
}
于 2020-06-07T06:07:09.167 回答