Can someone provide me with some basic code for asking an easy question that can be checked, for my Contact Form? . For example: "What color is the sky?". Thanks.
| SitePoint Sponsor |




Can someone provide me with some basic code for asking an easy question that can be checked, for my Contact Form? . For example: "What color is the sky?". Thanks.


Hy,
In your contact form add an input field:
Then, in the php file that gets data form, add:HTML Code:What color is the sky? Answer: <input type="text" name="ans" />
PHP Code:if(isset($_POST['ans']) && $_POST['ans']!='blue') exit('Wrong answer');
Free: Web Programming Courses HTML, CSS, Flash
Web Programming: AJAX Course and PHP-MySQL Course video Lessons
Good JavaScript and jQuery course for beginners




Thanks for that.
I appreciate it.
When the wrong answer is entered, the user sees a blank white page that shows only the words "Wrong answer".
Is their a way to have the words "Wrong answer" appear in the Contact Form
instead of seeing a blank page?
So, the user can simply see his error while still seeeing the Contact Form, and correct the answer conveniently?
Thanks.



If you use php's include function to include your data processing form on the top of your contact form script, then you can add in:
This way, we first assume that there's no error. If there's a post and it's wrong, then we say that there is an error. Next, we can use the following to print out the wrong answer (php's echo function will print the information to the screen).PHP Code:$errors = FALSE;
if(isset($_POST['ans']) && $_POST['ans']!='blue')
{$errors = TRUE;}
If you wanted to format Wrong answer, then you can do something like:PHP Code:if ($errors) {echo 'Wrong answer!';}
echo '<span class="my_wrong_answer_css_class">Wrong answer!</span>';
Hope this helps!
-Eric




I have a separate php handler from my contact form, so I guess when you ask
"If you use php's include function to include your data processing form on the top of your contact form script", the answer would be that I don't have it on top of the contact form, but on a seperate page (If I understand you correctly.)
So, would I used a different code than what you supplied?


Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form
Try your hand at the new JavaScript Challenge!
If you don't like getting your feet stuck in a bog, avoid Twitter BootsTrap.
If your site has a reasonable size, you might want to circulate a number of answers. Otherwise, once a spammer knows the answer, he can easily set up his script to add that answer. You can either serve a number of ready-made questions, or generate the questions on the fly. The easiest questions to generate are obviously math questions, but they are also the easiest to crack for a spammer.
Do you have other suggestions for good questions to ask? I don't like the idea of outright asking if my visitor is a human...
It should be something trivial and universal. For example, Are oranges purple? or Does sharks eat carrots? Something that every child can answer with ease, but which requires actual thinking.


Or, to write that properly:HTML Code:What color is the sky? Answer: <input type="text" name="ans" />
Anything less than that can be regarded as inaccessible.HTML Code:<label id="question" for="answer">Your question here</label> <input type="text" aria-labelledby="question" id="answer" [...other input attribute pairs...] >


Hello there Chris,
I see some nice answers up there at the start of the thread, good stuff—thanks to all for maintaining the quality of our fine community.
That's what I was going to say.Write a class that handles the questioning (if you do OOP) and add a number of questions to it, and then show a random one to the user.
As already mentioned above, with math questions you can generate them on-the-fly and there's no need for hard-coded questions. Downside it that it is very easy to go around it, as Ankerstjerne also mentioned. I can simply write a script that looks at the label (or wherever the numbers are) and calculates the right value and submits the form.
Besides, "real" questions are more fun for the user than math anyway.And thank you for considering this option instead of CAPTCHA—I just hate those letters and numbers that you have to enter over and over because you can't see what the hell they are.
![]()
Definitely wouldn't recommend yes/no questions, since there's then only two answers for a spammer to deal with. More like "What color is...?" kind of questions would be better. "What is the middle name of William Henry Gates?" — you get my point.
But the bottom line here (literally too) is to circle a bunch of questions around randomly to the user, otherwise it's way too easy to work around and you might as well skip the whole thing.![]()
Daniel Nordstrom. of. Nintera(ctive)
-- Featured post: Part 2. Writing NI.JS JavaScript
----- Follow me on Twitter. Got project? Contact me.
-------- SitePoint: Community Guidelines • Be A Great Member


While processing such captcha, make sure to lowercase the user's answer.
I mean, do not match with the case-sensitive answers.
Otherwise, some answers are likely to be wrong.
Bimal Poudel @ Sanjaal Framework » over Smarty Template Engine
ASKING INTERESTING QUESTIONS ON SITEPOINT FOURM
Hire for coding support - PHP/MySQL
I think asking a question is a much better idea than having a CAPTCHA. Even with good English skills, those CAPTCHA codes are so hard to readYou almost have to squint, and some characters look like they could be a number or a letter.

I agree with this 100%, but offer a word of caution.
There are questions you can ask that have ambiguous answers. That would lead to frustration from your users.
For example, *any* question whose answer is a mathematic value of zero could be answered as: NONE, ZERO, 0, Naught, NULL, NOTHING
If you are accepting textual answers (not only numbers) be sure to evaluate the answer in all lowercase (or all uppercase) so as to avoid an incorrect rejection because a user decided to capitalize.
True. This can be solved with a select, or with radiobuttons. This makes it somewhat easier to crack, but will still cut down spam significantly.
"What color is the sky?" is not really a good question. I mean right now the sky is black where I live because it's night time. And during the day it can be light-blue (or light blue), blue or even redish at sunset. I hope you get my point.

There is an alternative that I want to suggest. It's called hash cash and is totally invisible to humans and works great at stopping bots. PHP implementation example:
HashCash




Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form
Try your hand at the new JavaScript Challenge!
If you don't like getting your feet stuck in a bog, avoid Twitter BootsTrap.




Why not use the so honey-trap method? It is very effective. And the user does not have answer anything. Nor type. Give it a try.
------------------


That is not a method with which I can agree after experiencing the handiwork of someone who clearly did not know how to do it properly and accommodate false positives.
If a captcha is too hard to read or too strict on case etc then the coder is the problem not the idea of captcha. Questions will be limited and could encourage spammers to target your form with ease.




------------------

Math questions are easy to pose, and more users whose mother tongue is not yours probably more likely to understand the question.
Make sure the answers are between 1 and 999 and make the maxlength of the text area 3.
Ask questions using a mixture of words and numbers, the answers should always be numbers.
"What number is Thirty and 3?"
"What number is a dozen?"
Maintiain a single array of questions and answers which are available to the client-side generating code and the backend checking code:
turing_questions.php
Include the file in both the view and whatever you use to as a postback, set and retain control of which question is being posed in a session.PHP Code:<?php
$turing[0]['question'] = "Thirty and 3";
$turing[0]['answer'] = 33;
$turing[1]['question'] = "a dozen";
$turing[1]['answer'] = 12;
eg
Filtering and sanitizing the answer is dead simple.PHP Code:$_SESSION['turing'] = 1
PHP Code:<?php
// your postback form handler
include 'turing_questions.php';
if( !isset($_POST['answer'])
|| (int)$_POST['answer'] === 0
|| $_POST['answer'] > 999 )
// you are bad, sending you away unceremoniously
// $_SESSION['turing'] = 1
if( $_POST['answer'] !== $turing[$_SESSION['turing']]['answer'])
//failed to answer correctly, maybe you are not human,
// or able to answer
// mathematical questions or you do not speak my language?
//
Bookmarks