HaLo2FrEeEk Posted July 4, 2007 Share Posted July 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted July 4, 2007 Author Share Posted July 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 4, 2007 Share Posted July 4, 2007 If you are trying to check if a variable is empty, use the empty() function. Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted July 4, 2007 Author Share Posted July 4, 2007 I did and I fixed it, I didn't want to triple post. Thank YOU for posting though. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.