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
}

Edited by mostafatalebi
Link to comment
Share on other sites

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.

 

Link to comment
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.

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.