Home arrow PHP arrow Page 4 - Using Recaptcha with AJAX in PHP

PHP validation script: ajaxvalidate.php - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The first thing we need to check is the user's captcha. Recaptcha developers  suggest this validation script:

 

<?php

require_once('recaptchalib.php');

$errors=array();

$privatekey = "***Add your recaptcha private key here***";

$resp = recaptcha_check_answer ($privatekey,

                                $_SERVER["REMOTE_ADDR"],

                                $_POST["recaptcha_challenge_field"],

                                $_POST["recaptcha_response_field"]);

 

if (!$resp->is_valid) {
  die ("The reCAPTCHA wasn't entered correctly. Go back and try 
it again." .
       "(reCAPTCHA said: " . $resp->error . ")");
}

However, instead of terminating the script with die (), we will assign errors to a PHP array named $errors[] . With this approach we can dump all errors back to the users (along with field name error validation, not only the captcha) at once. So instead of having the die() command, we will have this:

if (!$resp->is_valid) {

  $errors[] = 'ERROR: The recaptcha code was not entered correctly or expired. If you think it expired or correctly entered; click Recaptcha refresh button <img src="/ajaxrecaptcha/getanewchallenge.jpg" /> to get a new challenge and press submit again.';

}

If an error occurs, the text value error will be assigned to the array. The rest of the validation script follows the same principle as above. For example, in checking to see if the name, phone number and age fields are empty:

if (empty($name)) {

$errors[] = 'ERROR: The name field is empty.';

}

if (empty($phonenumber)) {

$errors[] = 'ERROR: The phone field is empty.';

}

if (empty($age)) {

$errors[] = 'ERROR: The age field is empty.';

}

 

In the last part of the series of validation scripts, you need to determine how many errors are detected by the entire validation script. If there is no error, then proceed with the rest of the form processing (for example, displaying info back to the user, adding input to the MySQL database, etc). This can be accomplished using the PHP function sizeof(), which will count the number of entries in the array.

The script below does the actual work of dumping the errors back to the user:

if (sizeof($errors) > 0)

{

     echo "<ul>";

     foreach ($errors as $e)

     {

          echo "<li>$e</li>";

     }

     echo "</ul>";

     die ();

}

This code will be inserted after validation scripts:

<?php

 

/// Insert series of validation scripts here

 

if (sizeof($errors) > 0)

{

     echo "<ul>";

     foreach ($errors as $e)

     {

          echo "<li>$e</li>";

     }

     echo "</ul>";

     die ();

}

So for example, if mistakes are inputted in the form, it will display them all at once:

 

 

You can download the source code and see an actual working example.  



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap

Dev Shed Tutorial Topics: