weijihao / google-recaptcha
Easily use Google Recaptcha in CakePHP 3.2+ projects
Installs: 23
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Type:cakephp-plugin
Requires
- php: >=5.6
- cakephp/cakephp: ~3.2
Requires (Dev)
Suggests
- crabstudio/authenticate: Prevent Brute Force Attack
- crabstudio/email-queue: Allows you to simple send bulk email
README
Integrate Google Recaptcha v2 to your CakePHP v3.2+ project
Installation
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require crabstudio/recaptcha
Or add the following lines to your application's composer.json:
"require": {
"crabstudio/recaptcha": "^2.0"
}
followed by the command:
composer update
Load plugin
From command line:
bin/cake plugin load Recaptcha
Or this line to the end of Your_project\config\bootstrap.php
Plugin::load('Recaptcha');
Load Component and Configure
Override default configure from loadComponent:
$this->loadComponent('Recaptcha.Recaptcha', [
'enable' => true, // true/false
'sitekey' => 'your_site_key', //if you don't have, get one: https://www.google.com/recaptcha/intro/index.html
'secret' => 'your_secret',
'type' => 'image', // image/audio
'theme' => 'light', // light/dark
'lang' => 'vi', // default en
'size' => 'normal' // normal|compact|invisible
]);
Usage
Display recaptcha in your view:
<?= $this->Form->create()?>
<?= $this->Form->input('email')?>
<?= $this->Recaptcha->display()?> // Display recaptcha box in your view, if configure enable = false, nothing to display here
<?= $this->Form->submit()?>
<?= $this->Form->end()?>
Verify in your controller function
public function forgotPassword() {
if($this->request->is('post')){
if($this->Recaptcha->verify()) { // if configure enable = false, always return true
//do something here
}
$this->Flash->error(__('Please pass Google Recaptcha first'));
}
}
Invisible reCAPTCHA Configure
$this->loadComponent('Recaptcha.Recaptcha', [
'enable' => true, // true/false
'sitekey' => 'your_site_key', //if you don't have, get one: https://www.google.com/recaptcha/intro/index.html
'secret' => 'your_secret',
'type' => 'image', // image/audio
'theme' => 'light', // light/dark
'lang' => 'vi', // default en
'size' => 'invisible' // normal|compact|invisible
'callback' => 'onSubmit'
]);
your custom.js
function onSubmit(token) {
$("form#demo-form").submit();
}
$("button#demo-submit").click(function () {
event.preventDefault();
grecaptcha.execute();
});
Done