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

## 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']))
{

{
echo "Correct!";
}
else
{
}
}
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?

Jason

##### Share on other sites

Will you get a new c every time someone clicks submit?

##### Share on other sites

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

##### 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 a member or if the \$user_phrase is correct

if (\$_SESSION['pass_phrase']  == \$user_pass_phrase) {

echo "Correct!";

}

##### Share on other sites

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

##### 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.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.