LoginSignup
1
1

More than 5 years have passed since last update.

Google Recaptcha

Last updated at Posted at 2015-10-14

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

CakePHP 3: Integrate Google Recaptcha v2

Introduction

Integrate Google Recaptcha v2 to your CakePHP v3.x project

Donate

Buy me a cup of coffee paypal

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": "^1.0"
}

followed by the command:

composer update

Load plugin

Add this line to Your_project\config\bootstrap.php

Plugin::load('Recaptcha', ['bootstrap' => true]);

Configure

Write to your configure:

$recaptcha = [
'Recaptcha' => [
'type' => 'image', //available image/audio
'theme' => 'light', //available light/dark
'lang' => 'vi', //if not exist, plugin in will use your default locale
'enable' => true, //available true/false
'sitekey' => 'your_site_key', //if you don't have, get one: https://www.google.com/recaptcha/intro/index.html
'secret' => 'your_secret',
]
];
Cake\Core\Configure::write($recaptcha);

Load component

In your controller initialize function

// Example, i just use recaptcha for function forgotPassword
if($this->request->action === 'forgotPassword') {
$this->loadComponent('Recaptcha.Recaptcha');
}

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 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'));
        }
    }

Done

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1