danny3b / recaptcha
轻松在 CakePHP 3.2+ 项目中使用 Google Recaptcha
2.1.0
2018-04-26 16:16 UTC
Requires
- php: >=5.6
- cakephp/cakephp: ^3.5
Requires (Dev)
- cakephp/cakephp-codesniffer: ^3.0
- phpunit/phpunit: ^5.7.14|^6.0
Suggests
- crabstudio/authenticate: Prevent Brute Force Attack
- crabstudio/email-queue: Allows you to simple send bulk email
README
将 Google Recaptcha v2 集成到您的 CakePHP 项目中
安装
您可以使用 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
]);
用法
在您的视图中显示 recaptcha
<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Recaptcha->display() ?> // Display recaptcha box in your view, if configure enable = false, nothing to display here
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
如果 recaptcha 成功,则在回调中启用按钮
<?= $this->Form->button(__('Submit'), ['disabled']) ?>
<script>
function recaptcha_callback(){
$('button[type="submit"]').prop("disabled", false);
}
</script>
在控制器函数中进行验证
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'));
}
}
完成