reCAPTCHA plugin
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
- Put 'recaptcha_plugin' directory on APP/plugins in your app.
- Get 'recaptchalib.php' from Google code, and put it on recaptcha_plugin/vendors.
Place your reCAPTCHA keys on config/key.php .
$config = array(
'Recaptcha' => array(
'Public' => 'YOUR_RECAPTCHA_PUBLIC_KEY',
'Private' => 'YOUR_RECAPTCHA_PRIVATE_KEY',
),
);
Include plugin helper and component in your controller.
Controller Class:Add this code in your view.
public $components = array('RecaptchaPlugin.Recaptcha');
public $helpers = array('RecaptchaPlugin.Recaptcha');
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.

but validation is not working .
Please help
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]
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]
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!
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
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');
}
}
}
I fixed them and pushed to github.
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.
No, I have not seen yet. Thank you for telling me.
> is there any difference?
This plugin use reCAPTCHA Library.
https://github.com/CakeDC/recaptcha
Do you have not seen yet or is there any difference?