weijihao / google-recaptcha
轻松在 CakePHP 3.2+ 项目中使用 Google Recaptcha
2.0.9
2018-01-09 10:04 UTC
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
将 Google Recaptcha v2 集成到您的 CakePHP v3.2+ 项目中
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
安装 composer 包的推荐方法是
composer require crabstudio/recaptcha
或者将以下行添加到您的应用程序的 composer.json
"require": {
"crabstudio/recaptcha": "^2.0"
}
然后执行以下命令
composer update
加载插件
从命令行
bin/cake plugin load Recaptcha
或者将此行添加到 Your_project\config\bootstrap.php 的末尾
Plugin::load('Recaptcha');
加载组件和配置
从 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
]);
用法
在您的视图中显示 recaptcha
<?= $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()?>
在控制器函数中验证
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'));
}
}
不可见 reCAPTCHA 配置
$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'
]);
您的自定义.js
function onSubmit(token) {
$("form#demo-form").submit();
}
$("button#demo-submit").click(function () {
event.preventDefault();
grecaptcha.execute();
});
完成