Jump to content

[SOLVED] Can someone help me find the error please.


HaLo2FrEeEk

Recommended Posts

Hey, I created my own question based CAPTCHA to replace the normal one on my forum, but for some reason, question one is being stubborn.  It gets the questions fromt he array correctly, but when I type the answer, it doesn't work, it says it's wrong.  Can someone help me out please.

 

<?php
$questions = array("What is 2 + 2?", "What color is the sky?", "How many planets are there?", "What is 3 - 2?");
$answers = array("4", "blue", "9", "1");

switch($_REQUEST['act']) {
  case "secim":
    $question = $questions[$_REQUEST['ques']];

    $text_box = imagettfbbox(12, 0, $_SERVER['DOCUMENT_ROOT']."/forum/stats/bonzai.ttf", $question);
    $textwidth = $text_box[4] - $text_box[6];
    $textheight = $text_box[1] - $text_box[7];
    $im = imagecreate($textwidth+10, $textheight+10);
    $white = imagecolorallocate($im, 255, 255, 255);
    $black = imagecolorallocate($im, 0, 0, 0);
    imagefttext($im, 12, 0, 5, 5+$textheight, $black, $_SERVER['DOCUMENT_ROOT']."/forum/stats/bonzai.ttf", $question);
    imagepng($im);
    imagedestroy($im);
    break;
  case "check":
    $qid = $_POST['qid'];
    $answer = strtolower($_POST['ans']);
    if(!$qid || !$answer) {
      header("Location: secimage.php");
      }

    if($answer == $answers[$qid]) {
      echo "Your anwser was correct.";
      } else {
      header("Location: secimage.php");
      }
    break;
  default:
    $qid = rand(0, (count($questions)-1));

    echo "<img src=\"./secimage.php?act=secim&ques=".$qid."\">";
    echo <<<EOL
    <form method="POST" action="secimage.php?act=check">
    <input type="text" name="ans" />
    <input type="hidden" name="qid" value="$qid" />
    <input type="submit" value="Check answer" />
    </form>
EOL;
    break;
  }
?>

 

Question one "What is 2 + 2?" is the one giving the problems.  There are no errors, but it doesn't count the question as right.

Link to comment
Share on other sites

Ok, sorry for the double post.  I think I found it.  Where it says:

 

$qid = $_POST['qid'];
    $answer = strtolower($_POST['ans']);
    if(!$qid || !$answer) {
      header("Location: secimage.php");
      }

 

It is checking that $qid is not empty, but it is "0" for the first question, so it's reading it as a false.  I can think of one way to fix this, but it's a bit clumsy, can someone help me think of another way.  Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.