php中的代码行

时间:2010-11-11 00:50:41

标签: php

以下是代码......帮助我理解以下代码行...

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) {
        //no errors, do other actions here, like saving into database, send email or...
        echo "<span style='color:red'>Thank you for completing the form.
We shall contact you soon.</span>";
        die();

4 个答案:

答案 0 :(得分:1)

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

如果参数register已作为POST变量传递

,则仅执行以下操作
    $error = array();

声明一个空数组$error

    if (!in_array(strtolower($_POST['captcha']), $aCaptcha[$_SESSION['captcha']])) {
        $error['captcha'] = "<span style='color:red'>The name of the animal is not correct.</span>";
    }

如果提交的CAPTCHA不在包含有效CAPTCHAS列表的数组中 ,向数组$error

添加错误
    if (count($error) == 0) {
    //no errors, do other actions here, like saving into database, send email or...
    echo "<span style='color:red'>Thank you for completing the form.
We shall contact you soon.</span>";
    die();

如果没有错误,请打印成功消息并退出脚本。

答案 1 :(得分:1)

if the `register` variable exists in the post array (meaning the form was POSTed)
   declare an empty array and assign it to the error variable

   if the converted-to-lowercase posted value of the captcha is not the same as the captcha for this session from the aCaptcha array
       populate the error array with a message letting the user know that what they typed is wrong.

   if the error array is empty
      //no errors, do other actions here, like saving into database, send email
      print a success message
      exit the script.

我刚注意到这实际上是您在image captcha in php提出的较长问题的转贴

答案 2 :(得分:0)

这段代码只是一个小片段。

但看起来它是实现Captcha http://en.wikipedia.org/wiki/CAPTCHA

的代码

因此,该人试图注册,发送他/她看到的动物的名称,这部分代码基本上检查输入的动物名称是否正确输入。因为如果这封信大或小,它就会忽略。如果验证码是正确的,那么它表明注册是正常的。如果没有,则显示错误。

似乎它在一页上显示了动物的图片,然后你必须输入动物的名字。表单通过POST在变量验证码中发送输入的名称。在会话中,服务器存储了哪些图片($ _SESSION ['captcha'])

$ aCaptcha似乎是所有动物的阵列。

答案 3 :(得分:0)

/*
 * checks if the POST variable 'register' is set
 */
if (isset($_POST['register'])) {

  /*
   * create an empty array called $error
   */
  $error = array();

  /*
   *  Converts the POST variable 'captcha' to lowercase then
   *  checks if the POST variable 'captcha' is not in the $aCaptcha array.
   *  If it is not in the array it adds the error message to the $error array with key '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>";
  }

  /*
   * If the $error array is empty then there are no errors so display a thank you message
   */
  if (count($error) == 0) {
    //no errors, do other actions here, like saving into database, send email or...
   echo "<span style='color:red'>Thank you for completing the form. We shall contact you soon.</span>";
  }

  /*
   * Stops any more script execution.
   */
  die();
}

/*
 * There were a couple of missing closing tags there, 
 * im not sure if that was intentional by you or not 
 * but I have added them.
 */