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

PHP web form scripting - 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 default web form will look like this:

 

For simplicity it asks for four pieces of information from the user: name, phone, age and the captcha input using Recaptcha. The explanation and process is basically similar to the previousAJAXweb form  we discussed. 

However, since we are using Recaptcha, we will revise the old captcha from:

<img src="/ajaxwebform/captcha.php" />

<br />

Enter the Captcha as shown above:

<br /> <br />

<input style="background-color: #FFFFC0" type="text" name="captcha" id="captcha" size="10">

<br /> <br />

Into this script:

<?php

require_once('recaptchalib.php');

$publickey = "***Your own website recaptcha public key here***";

echo recaptcha_get_html($publickey);

?>

The PHP recaptcha script to be added takes care of displaying the captcha image to the user. Since you are using PHP scripts, it is important that the file name extension uses .php rather than .htm or something else. You will need to add your own recaptcha public key and change the value of the $publickey variable:

$publickey = "4Ld1R3434343KJKJKJMGylVnGFGFGFz1R8NraDDDDDQW";

Finally, in the AJAX JavaScript code, you will also be sending the Recaptcha parameters viaAJAXto the PHP validation script (ajaxvalidate.php). To add, we change the previous JavaScript code mentioned in the article from:

<script type="text/javascript">

function sendRequest() {

new Ajax.Request("/ajaxwebform/ajaxvalidate.php",

{

method: 'post',

parameters: 'name='+$F('name')+'&phonenumber='+$F('phonenumber')+'&age='+$F('age')+'&captcha='+$F('captcha'),

onComplete: showResponse

});

}

function showResponse(req){

$('show').innerHTML= req.responseText;

}

</script>

Into this:

<script type="text/javascript">

function sendRequest() {

new Ajax.Request("/ajaxrecaptcha/ajaxvalidate.php",

{

method: 'post',

parameters: 'name='+$F('name')+'&phonenumber='+$F('phonenumber')+'&age='+$F('age')+'&recaptcha_challenge_field='+$F('recaptcha_challenge_field')+'&recaptcha_response_field='+$F('recaptcha_response_field'),

onComplete: showResponse

});

}

function showResponse(req){

$('show').innerHTML= req.responseText;

}

</script>

 

Recaptcha variables are passed in the aboveAJAXparameters:

 ... +'&recaptcha_challenge_field='+$F('recaptcha_challenge_field')+'&recaptcha_response_field='+$F('recaptcha_response_field'),

 

These variables are recaptcha_challenge_field and recaptcha_response_field.

Those are the only changes made to the originalAJAXweb form. We will save this as index.php.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: