PHP Developer

My notes and thoughts about Linux, WordPress, PHP, WPML, Toolset and many more.

Implement reCaptcha without using recaptchalib.php plugin

This is a PHP tutorial on using reCaptcha without its library. Make sure though that you have secured your private and public key for your website. If you still have not got these keys, apply here.

One important thing you need to know is that your website private and public keys for recaptcha will still work in development/local host settings without the need to define it globally.

I received some complaints from various users of my script about the reCaptcha not working on their localhost environment. I confirm this to be true with my Ubuntu localhost setup.

Using reCaptcha without its library did fix the issue regardless of whether the script is now implemented in localhost or production server. OK let’s get started:

The HTML Web Form with reCaptcha

In your webform you can include the reCaptcha using the example below:

As you can see, there is no reference to recaptchalib.php. It uses JavaScript and iFrame to display the captcha on your form (served from the reCaptcha servers). Make sure you have replaced “put_your_public_key” here with your own value.

PHP Code to Check if reCaptcha is correct

After form submission, you need to verify if the answer is correct. Of course, the reCaptcha verification script should also not be using recaptchalib.php. This is done by calling the reCaptcha API using the example below:

Notes:

1.) The API will answer true if the answer is correct otherwise false. The PHP script particularly the strstr function above will check the presence of either true or false in the API response. If true is detected, the captcha validation is correct otherwise false.

2.) The example above uses file_get_contents to retrieve the API response. I believe its also possible with other methods.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">