简体   繁体   English

在PHP中的图像验证码

[英]image captcha in php

Below is the source code of a program. 下面是程序的源代码。 Can anyone help me to figure out the working of a program. 谁能帮我弄清楚程序的工作原理。

<?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>

On the first page 在第一页

  1. random number between 1 and 7 is generated and stored in session 生成1到7之间的随机数并将其存储在会话中
  2. form is displayed 显示表格
  3. picture in the captcha directory is displayed based on the random number 根据随机数显示验证码目录中的图片

On the second page 在第二页

  1. array with acceptable answers is generated - the keys are numbers 1 and 7 and the values are arrays of acceptable answers 生成具有可接受答案的数组-键是数字1和7,值是可接受答案的数组
  2. the below code checks that the answer given by the user $_POST['captcha'] is one of the acceptable answers $aCaptcha[$_SESSION['captcha']] 下面的代码检查用户$_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. if acceptable then a message is printed out and PHP stops executing 如果可以接受,则打印一条消息,PHP停止执行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM