php中的图像验证码

时间:2010-11-10 23:49:02

标签: php

以下是程序的源代码。任何人都可以帮我弄清楚程序的运作情况。

<?php
session_start();
?>
<?php
  $aCaptcha = array (
        array(),
        array('crocodile'),
        array('panda', 'panda bear', 'giant panda'),
        array('pig'),
        array('tiger'),
        array('zebra'),
        array('cow'),
        array('elephant')
    );

if (isset($_POST['register'])) {
    $error = array();
        if (!in_array(strtolower($_POST['captcha']), $aCaptcha[$_SESSION['captcha']])) {
        $error['captcha'] = "<span style='color:red'>The name of the animal is not correct.</span>";
    }
    if (count($error) == 0) {
                    echo "<span style='color:red'>Thank you for completing the form.
We shall contact you soon.</span>";
        die();
    }
}
?>
<form action="index.php" method="post">
    <?php
            $_SESSION['captcha'] = rand(1, 7);
    ?>  
<td colspan="3"><strong>Contact Form</strong></td>

             <p>Full Name :                   <input type="text" name="Nmaes" value='' />
<p>Mobile No. :                   <input type="text" name="Nmaes" value='' />
             <p>Email id :                   <input type="text" name="Nmaes" value='' />
             <p>Subject :                   <input type="text" name="Nmaes" value='' />
 <p>Message :                   <input type="text" name="Nmaes" value='' />
    <p><img src="<?php echo $path;?>captcha/<?php echo $_SESSION['captcha'];?>.jpg" /></p>
    <p>Type the name of the animal you see in the picture above. <input type="text" name="captcha" value='' />
    <?php echo(isset($error['captcha']))?$error['captcha']:"";?></p>
    <p><label>&nbsp;</label><input type='submit' name='register' value='register' /></p>
</form>

1 个答案:

答案 0 :(得分:0)

在第一页

  1. 生成1到7之间的随机数并存储在会话
  2. 表格显示
  3. 验证码目录中的
  4. 图片基于随机数
  5. 显示

    在第二页

    1. 生成具有可接受答案的数组 - 键是数字1和7,值是可接受答案的数组
    2. 以下代码检查用户$_POST['captcha']给出的答案是否为可接受的答案之一$aCaptcha[$_SESSION['captcha']]

      if (!in_array(strtolower($_POST['captcha']), $aCaptcha[$_SESSION['captcha']])) {
      $error['captcha'] = "<span style='color:red'>The name of the animal is not correct.</span>";
      
    3. 如果可以接受,则打印出一条消息,PHP停止执行

      echo "<span style='color:red'>Thank you for completing the form. We shall contact you soon.</span>";
      die();