I can't work out where to add reCAPTCHA

From Unofficial reCAPTCHA Wiki

Jump to: navigation, search

This advice applies to everyone who's integrating reCAPTCHA - irrespective of language.

Firstly you need to identify two hot spots in your code. These are the places where you'll be adding the reCAPTCHA calls.

Contents

[edit] Display reCAPTCHA

The first hot spot is the code that generates the form. Hopefully that's easy enough to find and you have been able to add the call to recaptcha_get_html() so that the captcha is displayed on your form.

[edit] Check reCAPTCHA

The second hot spot is the one that seems to be causing people problems.

You need to identify the code that handles form submission - that is the code that runs when the user clicks on submit.

It's hard to generalise about what this code looks like but here are some clues that may help you:

  • if your form can ever display an error message, for example if a required field is blank, look for the code that generates that error message.
  • if your form places information in a database look for code that does a SQL INSERT.
  • if your form sends mail search for the code that does that.

[edit] Form Already Has Validation

Life will be easier if your form already has some validation logic (i.e. it handles invalid input from the user by displaying a message instead of performing whatever action it normally would).

If that's the case and you can find that code that does that you're nearly home free. If the form can display an error message search for the text of the error message within the code and you're sure to be close.

Once you've found the validation code you need to add a call to recaptcha_check_answer() in addition to whatever other checks are there (email address blank? age too young? etc).

Please now consult How to integrate reCAPTCHA and the API documentation here:

http://recaptcha.net/plugins/php/

[edit] Form Has No Validation

If there's currently no validation logic for your form - if clicking on submit *always* succeeds - then you have a more tricky problem.

In that case you'll have to add the reCAPTCHA validation code right before the existing form handling code does whatever it does when the user submits. You'll also have to figure out how to redisplay the form to give the user another attempt at the captcha.

Unfortunately there's no one stock answer to how you do that. It depends entirely on the structure of the code you already have. If the existing code isn't too complex you may have luck posting it on the reCAPTCHA discussion group and asking for advice.

Personal tools