Captcha with Mootools


/ Published in: JavaScript
Save to your folder(s)

A simple JSON request to validate a captcha control (using the php plugin provided by Recaptcha)


Copy this code and paste it in your HTML
  1. /**
  2.  * author : Stephane P. Pericat
  3.  * date : 2010-03-01
  4.  */
  5.  
  6. PHP Code:
  7.  
  8. <?php
  9.  
  10. require "recaptcha/recaptchalib.php";
  11.  
  12. if($_POST) {
  13. $resp = recaptcha_check_answer(
  14. "6LdrkwsAAAAAAHX2b45bn_...", //put your recaptcha private key here
  15. $_SERVER["REMOTE_ADDR"],
  16. $_POST["recaptcha_challenge_field"],
  17. $_POST["recaptcha_response_field"]
  18. );
  19. if ($resp->is_valid) {
  20. echo json_encode(true);
  21. } else {
  22. echo json_encode(false);
  23. }
  24. }
  25. ?>
  26.  
  27. //-----------------------------------------------
  28.  
  29. Javascript front-end:
  30.  
  31. new Asset.javascript('http://api.recaptcha.net/js/recaptcha_ajax.js');
  32.  
  33. $(document.body).addEvent('submit', function(event) {
  34. event.stop();
  35. var challenge = Recaptcha.get_challenge();
  36. var response = Recaptcha.get_response();
  37. var control = new Request.JSON({
  38. url: 'control.php',
  39. data : {
  40. recaptcha_challenge_field : challenge,
  41. recaptcha_response_field : response
  42. },
  43. method: 'post',
  44. onComplete : function (result) {
  45. if(result == true) {
  46. Recaptcha.destroy();
  47. console.log(result);
  48. } else {
  49. Recaptcha.reload();
  50. }
  51. }
  52. }).send();
  53. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.