Jump to content

VERY simple math... i'm brain dead today


MainStWebGuy

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/268377-very-simple-math-im-brain-dead-today/
Share on other sites

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!";

}

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.