Home arrow PHP arrow Using Recaptcha with AJAX in PHP

Using Recaptcha with AJAX in PHP

With bots getting intelligent enough to read easy versions of captcha, it's time to make your site a little trickier if you want to avoid spam. This article will show you how to set up a PHP web form with AJAX that uses Recaptcha, a more difficult protocol for bots to decipher.

  1. Using Recaptcha with AJAX in PHP
  2. Form design, planning and implementation
  3. PHP web form scripting
  4. PHP validation script: ajaxvalidate.php
By: Codex-M
Rating: starstarstarstarstar / 4
January 28, 2010

print this article



After a previous tutorial on PHP Ajax web forms, there were a lot of good points raised by the readers which could dramatically improve the usability and performance of any PHP AJAX web form validation. 

Any good PHP web form should be able to accomplish the following important objectives: 

1. The form validation should display all errors at once. The original web form discussed in the tutorial mentioned above can't currently display all errors found in the PHP server side validation. As a result, it will only display a single error at a time. This is due to the use of the die () command. For example:

if ($countfirstname <2) {

echo 'ERROR: You are using an invalid first name, it should contain more than one character.';

die ();



The die () command terminates the execution of the PHP script and displays the error back to the browser. 

2. The captcha needs to be difficult. Modern bots have evolved to read easy captchas, according to Wikipedia. Fulfilling a need for a more challenging captcha like Recaptcha ( ) is important. 

3. Users should able to easily change wrong information entered into the form without needing to press the browser's back button. This is whereAJAX-enabled web forms are useful. 

4. Combining a PHP web form withAJAXvalidation means doing server-side validation of user inputs without the need for client validation like JavaScript (becauseAJAXitself is based in JavaScript). This eliminates the risk of spoofing common in-client validation.

Main requirements 

Unfortunately this type of Recaptcha AJAX in PHP will not work in all hosting accounts. It requires:

1. That an important PHP function for communicating with Recaptcha server be enabled. An example of this function includes Fopen. Typically these are disabled in free hosting accounts for security reasons. However, if you have a paid hosting account, then you need to find out if these are enabled by checking php.ini file.

2. A Recaptcha account for your own website. You should register it by signing up for Recaptcha for free. You need two important keys which you will get after signing up, and it is recommended that you copy these keys to a text file like notepad for your personal records. These keys are the Public Key and the Private Key. Without these important keys, your PHP web form usingAJAXand Recaptcha will not work.

>>> More PHP Articles          >>> More By Codex-M

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: