开发者

line of codes in php

Below is the code...help me in understanding the below codes of line...

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>";
    }
开发者_Python百科    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();


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

Only do the following if the parameter register has been passed as a POST variable

    $error = array();

Declare an empty 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>";
    }

If the CAPTCHA submitted is not in the array that contains the list of valid CAPTCHAS , adds an error into the array $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();

If there are no errors, print a success message and exit the script.


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.

I just noticed that this is actually a repost of a longer question you asked at image captcha in php


this code is just a small snippet.

but it looks like it is code to implement a Captcha http://en.wikipedia.org/wiki/CAPTCHA

so the person tries to register, sends the name of the animal he/she sees and this part of code checks basically if the entered name of the animal is entered correctly. for that it ignores if the letter is big or small. if captcha was correct, than it show that registration is okay. if not, than it shows the errors.

it seems like it shows a picture of the animal on one page and then you have to enter the name of the animal. the form sends the entered name through POST in the variable captcha. In the session the server stored which picture was shown ($_SESSION['captcha'])

$aCaptcha seems to be the array of all animals.


/*
 * 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.
 */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜