You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require ivanamat/cakephp-captcha
git submodule add [email protected]:ivanamat/cakephp-captcha.git plugins/Captcha
git submodule init
git submodule update
Get reCAPTCHA secret at https://www.google.com/recaptcha
Set the secret in your config/bootstrap.php
file.
Configure::write('Captcha.secret','MY_SECRET_KEY');
Load component in the initialize()
function
class MyController extends AppController {
public function initialize() {
parent::initialize();
$this->loadComponent('Captcha.Captcha');
}
}
...or load the component in the array of components
.
class MyController extends AppController {
public $components = [
'Captcha' => [
'className' => 'Captcha.Captcha'
]
];
}
# MyController
$ip = getenv('REMOTE_ADDR');
$gRecaptchaResponse = $this->request->data['g-recaptcha-response'];
$captcha = $this->Captcha->check($ip,$gRecaptchaResponse);
if($captcha->errorCodes == null) {
// Success
} else {
// Fail! Maybe a bot?
}
Paste this snippet before the closing tag </head>
in the HTML template
<script src='https://www.google.com/recaptcha/api.js'></script>
Paste this snippet at the end of the <form>
where you want the reCAPTCHA widget to appear. Replace YOUR-SITEKEY
with your own site key.
<div class="g-recaptcha" data-sitekey="YOUR-SITEKEY"></div>
CakePHP 4.x - Captcha uses the reCAPTCHA third-party library.
You can download reCAPTCHA from official website: [https://github.com/google/recaptcha)
Iván Amat on GitHub and www.ivanamat.es
BusaniPrepaid on GitHub