Simple Math Question Captcha Component
I don’t like CAPTCHAs. I don’t know anyone who does.The downfalls of CAPTCHAs are many - hard to read, annoying, impossible for those with vision difficulties - and the benefits are slim. So, I came up with an alternative: a plain text math question to ask the user. This component generates a random equation and registers the answer as a session variable. The programmer can then check the form submitter’s answer against the session-registered answer using the validation function provided in the component.
Usage is really simple - I’ll run through putting it into an equally simple contact form, which looks a lot like the one done by Jonathan Snook (http://snook.ca/archives/cakephp/contact_form_cakephp/). The Contact model looks like this (app/models/contact.php): Find in attachment file.
No DB; manual schema; just a placeholder, really.
The Contact controller is set up like this (app/controllers/contact_controller.php): Find in attachment file.
So, we’ve added MathCaptcha to our list of components. There are various configuration options which you can set when adding MathCaptcha to the $components array - the config array (with defaults) looks like this:
private $__defaults = array(
'operand' => '+',
'minNumber' => 1,
'maxNumber' => 5,
'numberOfVariables' => 2
);
In the index() method, you can see the usage: if we’ve got a POST request, we call the component’s validates() method and pass to it the relevant data from the form - the user’s answer to the question. If it validates then we continue with the rest of the data validation, otherwise we give an error message. You’ll notice that the generateEquation() method is called regardless; we want a new question generated each time the page loads.
Finally, we just need one line in the view to grab the ’security_code’. Here’s the entire contact form (app/views/contact/index.ctp): Find in attachment file.
I’ve called the form field ’security_code’, but you can call it whatever you want.
And that’s it! A plain text math ‘captcha’ in almost no time.
More Details: http://jamienay.com/blog/featured/simple-math-question-captcha-component...
Recent comments