MainStWebGuy Posted September 14, 2012 Share Posted September 14, 2012 Hey guys, sorry to bother, but i'm brain dead today. I have a very simple script that i can't seem to get working. I am just generating two random numbers and asking the user to input the answer (spam check thingy), but i can't seem to get it going... here's what i'm using: <?php $a = rand(1, 10); $b = rand(1, 10); $c = $a + $b; ?> <p>Solve the problem to prove you're human:</p> <?php if(isset($_POST['submit'])) { $answer = $_POST['answer']; if($answer == $c) { echo "Correct!"; } else { exit("<p>Answer incorrect. Please hit back in your browser and try again</p>"); } } else { ?> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <p><?php echo $a;?> + <?php echo $b;?> = <input type="text" name="answer" size="3" /></p> <input type="submit" name="submit" /> </form> <?php } ?> could anyone please set me straight? thanks in advance! Jason Quote Link to comment https://forums.phpfreaks.com/topic/268377-very-simple-math-im-brain-dead-today/ Share on other sites More sharing options...
floridaflatlander Posted September 14, 2012 Share Posted September 14, 2012 Will you get a new c every time someone clicks submit? Quote Link to comment https://forums.phpfreaks.com/topic/268377-very-simple-math-im-brain-dead-today/#findComment-1377940 Share on other sites More sharing options...
MainStWebGuy Posted September 14, 2012 Author Share Posted September 14, 2012 AH HA! like i said... i am mildly retarded today... i'm going to go smack myself in the face, then move the variables inside the if statement that checks if submit was pushed... THANKS! Jason Quote Link to comment https://forums.phpfreaks.com/topic/268377-very-simple-math-im-brain-dead-today/#findComment-1377941 Share on other sites More sharing options...
floridaflatlander Posted September 14, 2012 Share Posted September 14, 2012 You may have to use sessions, something like this $a = rand(1, 10); $b = rand(1, 10); $c = $a + $b; $_SESSION['pass_phrase'] = SHA1($c); // $_POST user_phrase if (isset($_POST['answer'])) { $user_pass_phrase = sha1($_POST['answer']); } // if a member or if the $user_phrase is correct if ($_SESSION['pass_phrase'] == $user_pass_phrase) { echo "Correct!"; } Quote Link to comment https://forums.phpfreaks.com/topic/268377-very-simple-math-im-brain-dead-today/#findComment-1377944 Share on other sites More sharing options...
ignace Posted September 15, 2012 Share Posted September 15, 2012 That won't work as you may think it will. The pass_phrase will change on every page request. The correct would be: // $_POST user_phrase if (isset($_POST['answer'])) { $user_pass_phrase = sha1($_POST['answer']); // if a member or if the $user_phrase is correct if ($_SESSION['pass_phrase'] == $user_pass_phrase) { echo "Correct!"; } } $a = rand(1, 10); $b = rand(1, 10); $c = $a + $b; $_SESSION['pass_phrase'] = SHA1($c); Now the pass_phrase is not overwritten when you submit the form, so that you can actually validate it. Is the pass phrase not correct then you are presented with a new one. Quote Link to comment https://forums.phpfreaks.com/topic/268377-very-simple-math-im-brain-dead-today/#findComment-1378083 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.