crabstudio / recaptcha
Easily use Google Recaptcha in CakePHP projects
Installs: 92 607
Dependents: 2
Suggesters: 4
Security: 0
Stars: 21
Watchers: 4
Forks: 17
Type:cakephp-plugin
Requires
- php: >=8.1
- cakephp/cakephp: ^5.0
Requires (Dev)
- cakephp/cakephp-codesniffer: ^4.1
- phpunit/phpunit: ^10.5.5 || ^11.1.3
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 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
Load plugin
From command line:
bin/cake 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' => 'en', // default 'en' 'size' => 'normal' // normal/compact 'callback' => null, // `callback` data attribute for the recaptcha div, default `null` 'scriptBlock' => true // Value for `block` option for HtmlHelper::script() call ]);
Override default config from app config file:
// file: config/app.php /** * Recaptcha configuration. */ 'Recaptcha' => [ 'sitekey' => 'your_site_key', 'secret' => 'your_secret', 'type' => 'image', 'theme' => 'light', 'lang' => 'es', 'size' => 'normal' ]
Override default configure from recaptcha config file:
// file: config/recaptcha.php return [ /** * Recaptcha configuration. * */ 'Recaptcha' => [ 'enable' => true, 'sitekey' => 'your_site_key', 'secret' => 'your_secret', 'type' => 'image', 'theme' => 'light', 'lang' => 'es', 'size' => 'normal' ] ];
Load recaptcha config file:
// file: config/bootstrap.php Configure::load('recaptcha', 'default', true);
Config preference:
- loadComponent config options
- recaptcha config file
- app config file
Usage
Display recaptcha in your template:
<?= $this->Form->create() ?> <?= $this->Form->control('email') ?> // Display recaptcha box in your template, if configure has enable = false, nothing will be displayed <?= $this->Recaptcha->display() ?> <?= $this->Form->button() ?> <?= $this->Form->end() ?>
Verify in your controller function
public function forgotPassword() { if ($this->request->is('post')) { if ($this->Recaptcha->verify()) { // if configure enable = false, it will always return true //do something here } $this->Flash->error(__('Please pass Google Recaptcha first')); } }
Done.