crabstudio / recaptcha
轻松在 CakePHP 项目中使用 Google Recaptcha
4.0.0
2023-10-18 18:25 UTC
Requires
- php: >=8.1
- cakephp/cakephp: ^5.0
Requires (Dev)
- cakephp/cakephp-codesniffer: ^4.1
- phpunit/phpunit: ^10.1.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 update
加载插件
从命令行加载
bin/cake 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
]);
从应用程序配置文件中覆盖默认配置
file: config/app.php
/**
* Recaptcha configuration.
*
*/
'Recaptcha' => [
'enable' => true,
'sitekey' => 'your_site_key',
'secret' => 'your_secret',
'type' => 'image',
'theme' => 'light',
'lang' => 'es',
'size' => 'normal'
]
从 recaptcha 配置文件中覆盖默认配置
file: config/recaptcha.php
<?php
return [
/**
* Recaptcha configuration.
*
*/
'Recaptcha' => [
'enable' => true,
'sitekey' => 'your_site_key',
'secret' => 'your_secret',
'type' => 'image',
'theme' => 'light',
'lang' => 'es',
'size' => 'normal'
]
];
加载 recaptcha 配置文件
file: config/bootstrap.php
Configure::load('recaptcha', 'default', true);
配置偏好
- loadComponent 配置选项
- recaptcha 配置文件
- 应用程序配置文件
用法
在视图中显示 recaptcha
<?= $this->Form->create() ?>
<?= $this->Form->control('email') ?>
<?= $this->Recaptcha->display() ?> // Display recaptcha box in your view, if configure has enable = false, nothing will be displayed
<?= $this->Form->button() ?>
<?= $this->Form->end() ?>
在控制器函数中验证
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'));
}
}
完成