reCAPTCHA plugin

by tbsmcd

If you are victim for SPAM, then you'll want to deal easily.
This reCAPTCHA plugin will help you in that case.
https://github.com/tbsmcd/recaptcha_plugin

Features

Display reCAPTCHA widget and validate input value.

How to use

  1. Put 'recaptcha_plugin' directory on APP/plugins in your app.
  2. Get 'recaptchalib.php' from Google code, and put it on recaptcha_plugin/vendors.
  3. Place your reCAPTCHA keys on config/key.php .


    $config = array(
        'Recaptcha' => array(
            'Public'  => 'YOUR_RECAPTCHA_PUBLIC_KEY',
            'Private' => 'YOUR_RECAPTCHA_PRIVATE_KEY',
        ),
    );
  4. Include plugin helper and component in your controller.
    Controller Class:


    public $components = array('RecaptchaPlugin.Recaptcha');
    public $helpers = array('RecaptchaPlugin.Recaptcha');
    Add this code in your view.
    View Template:


    echo $this->Recaptcha->show();  // Display reCAPTCHA widget.
    echo $this->Recaptcha->error(); // Show validation message of reCAPTCHA.

    You must place these codes inside form tags.This helper use 'red' theme in default, but you can use the other themes.
    For example:

     
       echo $this->Recaptcha->show('white'); 
        // Possible value: 'red'|'white'|'blackglass'|'clean' ;

    What does it do

    This plugin adds validation rule to model class.

    RecaptchaComponent


    function beforeValidate(&$model) {
            $model->validate['recaptcha_response_field'] = array(
                'checkRecaptcha' => array(
                    'rule' => array('checkRecaptcha', 'recaptcha_challenge_field'),
                    'message' => 'You did not enter the words correctly. Please try again.',
                ),
            );
        }

    RecaptchaComponent adds ValidationBehavior to model class, so you don't have to place any codes in model class.

Report

More on Plugins

Advertising

Comments

  • siddhartha posted on 03/26/13 07:42:38 AM
    this is great work . it is working properly
    but validation is not working .
    Please help
    • siddhartha posted on 03/26/13 11:20:33 AM
      this is great work . it is working properly
      but validation is not working .
      Please help

      showing error

      Strict (2048): Declaration of ValidationBehavior::beforeValidate() should be compatible with ModelBehavior::beforeValidate(Model $model) [APP\Plugin\ReCaptcha\Model\Behavior\ValidationBehavior.php, line 2]
  • maheshramasamy posted on 01/22/13 02:47:49 PM
    In Version 2.x , it shows following error.

    Strict (2048): Declaration of RecaptchaComponent::startup() should be compatible with Component::startup(Controller $controller) [APP/Plugin/recaptcha_plugin/Controller/Component/RecaptchaComponent.php, line 17]
    Strict (2048): Declaration of ValidationBehavior::beforeValidate() should be compatible with ModelBehavior::beforeValidate(Model $model) [APP/Plugin/recaptcha_plugin/Model/Behavior/ValidationBehavior.php, line 2]
  • johnd posted on 10/16/11 02:44:03 AM
    Thanks for the great plugin. It works for me. However, when I try to create static pages (create a new .ctp file in the /app/pages/ dir), I get the following error:

    Notice (8): Undefined property: PagesController::$Page [APP/plugins/recaptcha_plugin/controllers/components/recaptcha.php, line 12]
    Notice (8): Trying to get property of non-object [APP/plugins/recaptcha_plugin/controllers/components/recaptcha.php, line 12]

    Fatal error: Call to a member function attach() on a non-object in /app/plugins/recaptcha_plugin/controllers/components/recaptcha.php on line 12

    When I remove the plugin it works. It seems like the problem is here:
    $controller->$modelClass->Behaviors->attach('RecaptchaPlugin.Validation');

    Since the pages_controller.php doesn't use a model, so $modelClass is null. Any suggestions anyone?

    Thanks!

  • DaveyM posted on 06/17/11 03:41:56 PM
    Hi,

    I had a problem with updating model records within the controller that was using the RecaptchaPlugin.Recaptcha component.

    Following the save of some data submitted by a form (which featured recaptcha)
    $this->Card->save($this->data)

    I wanted to send an email using that data, and then, if the email sent successfully, set a flag against the record in the database.


    $this->Card->set('email_sent', true);
    $this->Card->save();


    However, I think the lack of 'recaptcha_response_field' when performing the second call to save caused validation to fail and my flag wouldn't save. So, to get around this
    I used the beforeValidate callback in my model.


    class Card extends AppModel {
    var $name = 'Card';

    function beforeValidate() {
    if (!isset($this->data['_Token'])) {
    unset($this->validate['recaptcha_response_field']);
    }
    }

    ...
    }


    This works because I am using the Security component which creates the _Token hidden field in my forms.

    I hope this helps someone,

    David
  • Jahdrien posted on 04/07/11 04:43:04 PM
    I also modified your component so that it won't validate if you left recaptcha response field empty. You also need to add 'allowEmpty' => false to your validation rule.

    class RecaptchaComponent extends Object {
    function startup(&$controller) {
    if (isset($controller->params['form']['recaptcha_challenge_field']) && isset($controller->params['form']['recaptcha_response_field'])) {
    $modelClass = $controller->modelClass;
    $controller->data[$modelClass]['recaptcha_challenge_field'] = $controller->params['form']['recaptcha_challenge_field'];
    $controller->data[$modelClass]['recaptcha_response_field'] = $controller->params['form']['recaptcha_response_field'];
    $controller->$modelClass->Behaviors->attach('RecaptchaPlugin.Validation');
    }
    }
    }
  • tbsmcd posted on 02/16/11 07:59:03 AM
    Thank you.
    I fixed them and pushed to github.
  • herod posted on 02/08/11 11:57:10 AM
    Hi,
    You did some mistake in helpers/recaptcha line 6 change VENDERS to VENDORS
    And as an addon i would suggest that in the model/behaviour/validation you also put required true otherwise it will just pass if you have nothing entered in the text field of captcha.
    thanks.
  • tbsmcd posted on 02/05/11 03:17:22 PM
    > Do you have not seen yet
    No, I have not seen yet. Thank you for telling me.
    > is there any difference?
    This plugin use reCAPTCHA Library.
  • hiromi posted on 02/05/11 02:14:04 PM
    There is already public Recaptcha Plugin from CakeDC:
    https://github.com/CakeDC/recaptcha
    Do you have not seen yet or is there any difference?
login to post a comment.