Jump to content

Recommended Posts

I want the user to answer a little calculation as an anti-spam measure but can't get it to work. Here's my code...

 

<?php
// create an anti-spam calculation to be answered
$first_num = rand(1, 3);
$second_num = rand(1, 3);
$multiplier = rand(2,3);

$actual_answer = ($first_num + $second_num) * $multiplier;


if($_SERVER['REQUEST_METHOD'] == 'POST') {
if($_POST['user_answer'] !== $actual_answer) {
echo "You didn't answer the anti-spam calculation correctly.";
} else {
echo "Correct!";
}
} else {
?>
<form method='post' action=''>
<?php echo "($first_num + $second_num) x $multiplier) = "; ?>
<input type='text' name='user_answer' id='user_answer' /><br /><br />
<input type='submit' name='submit' value='go' />
</form>
<?php
}

 

Can anybody tell me what I'm doing wrong? Thanks in advance for your help.

each time the page loads $actualanswer is being recalculated, so the odds of it matching the post answer from the last generation are pretty slim. you need to pass the actual answer, either in a session or in a hidden form element, along with the given answer to compare the correct $actualanswer with the one given

Thanks, Muddy_Funster. Got it working with this...

 

<?php
session_start();
// create an anti-spam calculation to be answered
$first_num = rand(1, 3);
$second_num = rand(1, 3);
$multiplier = rand(2,3);

if($_SERVER['REQUEST_METHOD'] == 'POST') {
if($_POST['user_answer'] !== $_POST['actual_answer']) {
echo "You didn't answer the anti-spam calculation correctly.";
} else {
echo "Correct!";
}
} else {


?>
<form method='post' action=''>
<?php echo "($first_num + $second_num) x $multiplier) = "; ?>
<input type='hidden' name='actual_answer' id='actual_answer' value="<?php echo $_SESSION['actual_answer'] = ($first_num + $second_num) * $multiplier;?>" />
<input type='text' name='user_answer' id='user_answer' /><br /><br />
<input type='submit' name='submit' value='go' />
</form>
<?php
}

Not the best of solutions, as now the bots doesn't even have to guess: They can just read the correct answer from the HTML code directly...

 

The correct way to do this would have been to save the correct answer in the session, or at least as a salted and hashed value in the form. Then only recalculate the correct answer once it's been established that the form needs to be shown again. Either because the previous answer was wrong, or because no form has been posted yet.

Not the best of solutions, as now the bots doesn't even have to guess: They can just read the correct answer from the HTML code directly...

Nonsense.

 

Using both a hidden input and a session is a bit of overkill here. Storing the value in a session will be fine.

 

<?php
session_start();
// create an anti-spam calculation to be answered
$first_num = rand(1, 3);
$second_num = rand(1, 3);
$multiplier = rand(2,3);

if($_SERVER['REQUEST_METHOD'] == 'POST') {
if($_POST['user_answer'] !== $_SESSION['actual_answer']) {
echo "You didn't answer the anti-spam calculation correctly.";
} else {
echo "Correct!";
}
} else {
$_SESSION['actual_answer'] = ($first_num + $second_num) * $multiplier;


?>
<form method='post' action=''>
<?php echo "($first_num + $second_num) x $multiplier) = "; ?>
<input type='text' name='user_answer' id='user_answer' /><br /><br />
<input type='submit' name='submit' value='go' />
</form>
<?php
}

AyKay47: I'm afraid it's not nonsense, if you read his code again you'll see for yourself. He's echoing the unmodified result into the HTML form, even as a hidden input it's clearly available for anyone who looks. The session variable isn't even used in his script, other than to simply save the answer (and nothing else).

 

Your code, however, fixed that issue. As specified in my post.

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.