Captcha应为True时返回False

时间:2015-01-19 11:46:47

标签: php

为什么我的Captcha应该返回时为False

当我第一次提交表单时,如果答案正确,则表示“验证码”不合适。当我第二次再次提交正确的答案时,它说“验证码”很好。有什么想法吗?

<?php

if ($_POST['submitted']==1) {

  $submitted = 1;

  $captchas = Array();

  # Create a single entry
  $captchas[] = array(
      "question" => "What colour is the sky?",
      "answer" => "Blue",
      "options" => array("Blue", "Red", "Orange")
  );


  $tabindex = 5;
  $captcha_index = rand(0, count($captchas) - 1);

  $comment_captcha_idx  = trim($_POST['captcha_index']);
  $comment_captcha = trim($_POST['captcha']);

  if ( !is_numeric($comment_captcha_idx) || !$comment_captcha || $captchas[$comment_captcha_idx]["answer"] != $comment_captcha)
      {
        $errormsg[] = "CAPTCHA";
      } 

      if ($errormsg == '') { 

        echo 'captcha fine';

      } else {

        echo 'captcha not fine';

      }

  }
?>

<div>

<form action="test.php" method="post">

<input type="hidden" name="captcha_index" value="<?php echo $captcha_index; ?>" />
      <hr />
      <h2>Captcha</h2>

<?php 

        $captchas = Array();

        # Create a single entry
        $captchas[] = array(
            "question" => "What colour is the sky?",
            "answer" => "Blue",
            "options" => array("Blue", "Red", "Orange")
        );

        $tabindex = 5;
        $captcha_index = rand(0, count($captchas) - 1);

        $comment_captcha_idx  = trim($_POST['captcha_index']);
        $comment_captcha = trim($_POST['captcha']);

      echo $captchas[$captcha_index]["question"]; 
      ?>
      <?php 
      foreach ($captchas[$captcha_index]["options"] as $captcha_answer) { 
      ?>
      <br /><label for="captcha_<?php echo $captcha_answer; ?>">
      <input tabindex="<?php echo $tabindex++; ?>"
          id="captcha_<?php echo $captcha_answer; ?>" type="radio"
          name="captcha" value="<?php echo htmlspecialchars($captcha_answer); ?>"
          /><?php echo $captcha_answer; ?></label>
      <?php
      }
      ?>
      </p>

      <p>
        <input type="submit" value="Send my order" name="Submit" />
        <input type="hidden" name="submitted" value="1" />
      </p>
      <p>&nbsp;</p>

     </form>
    </div>

0 个答案:

没有答案