Jump to content

Executing The Function Only Once (For Captcha)


mostafatalebi

Recommended Posts

Hello everybody

I have a problem in my form submission which I think could be easily solved by a pro programmer

 

Setting:

I have a form

At the end of the form I have put something like a CAPTCHA. There is a question, and a text input for the user to enter the answer.

So far everything works fine, the question is randomly generated without any problem.

 

But as soon as the user clicks on the submit button, the script regenerates the question which naturally has a different answer than the one the user has entered. What should I do to avoid execution of the function when the form is submitted??

 

However if somebody likes to see the script architecture here it is:

 

function randomSentenceGenerator ()
{
php script
}
$question_and_answer_array = randomSentenceGenerator();
if(isset($_POST['submit'])
{
if(question != answer)
{
tell user to enter a correct answer
}
Include the HTML
}
else
{
include the HTML
}

logic is the same as you expect, but still doesn't

 

Your logic is not as one would expect, it is incorrect.  Using your above template as an example, the correct logic flow would be something like:

function randomSentenceGenerator ()
{
php script
}




if(isset($_POST['submit'])
{
if(question != answer)
{
	tell user to enter a correct answer
}
Include the HTML
}
else
{
$question_and_answer_array = randomSentenceGenerator();
include the HTML
}

 

You generate a question only if the form was not submitted (or, optionally, if they answer incorrectly). When the form has been submitted you compare the post'ed answer with the answer in the session.

 

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.