inimist/cakephp-captcha
Composer 安装命令:
composer require inimist/cakephp-captcha
包简介
CakePHP Captcha Plugin, Image, Math and Google Recaptcha Support
README 文档
README
Bot Detect using Image captcha, Math captcha and Google-recaptcha for CakePHP 3
Why to use this plugin?
- To display Google Recaptcha / Image Captcha / Math Challenge for web forms
This CakePHP Captcha plugin requires CakePHP 3.6+.
Quick Features
- No Custom Code required.
- Quick install. Just add 3 lines, on each in Controller, Model and View.
- Works as Model Validation.
- Multiple Captchas on single page.
- Multiple Demo available.
- Demo available as CakePHP Plugin.
Installation
composer require inimist/cakephp-captcha
and
bin/cake plugin load Captcha -b -r
Implementation
-
Load Captcha plugin.
If you ran
bin/cake plugin load Captcha -b -rabove skip this step.Place
Plugin::load('Captcha');in your application's Application.php or bootstrap.php file. -
Load Capthca component
Place
$this->loadComponent('Captcha.Captcha');in your controllerr's initialize functionOR
Load Captcha component on the fly, in the particular controller action function. For example in the signup() action:
$this->loadComponent('Captcha.Captcha'); //or load on the fly!$ -
Add Behavior to your Model/Table
Place
$this->addBehavior('Captcha.Captcha', ['field'=>'<fieldname>'])in your Model (Table class) Note: If you use Google Recaptcha add "secret" option with its value which you get from Google. Also, add Google site key in the view file. -
Create an input field in your view's form as:
echo $this->Captcha->create('<fieldname>', $options); -
In your controller in which your form data is processed, place (required with image and math catpcha):
$this->Users->setCaptcha('<fieldname>', $this->Captcha->getCode('<fieldname>'));just before patching entity. For example:
$this->Users->setCaptcha('securitycode', $this->Captcha->getCode('securitycode')); $user = $this->Users->patchEntity($user, $this->request->data);
A fully working demo can be found here. You can install a fully working demo as a plugin from here.
Settings
The best place for settings is your_apps/config/app.php file. Create a new key named "Captcha" and specify settings there.
'Captcha' => [
'type' => 'recaptcha',
'sitekey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
//'clabel' => 'Enter code',
//'reload_txt' => 'Reload??'
]
More examples
Options:
$options['width']=150;
$options['height']=50;
$options['theme']='default';
echo $this->Captcha->create('captcha_input_field_name', $options);
Multiple Captchas on same page:
//form 1
$options1['width']=150;
$options1['height']=50;
echo $this->Captcha->create('captcha_input_field_name1', $options1);
//form 2, A math captcha, anywhere on the page
$options2['type']='math';
echo $this->Captcha->create('captcha_input_field_name2', $custom2);
Options for view template. Ex: $this->Captcha->create('field_name', $options):
- field: field name (Optional. Default "captcha")
- type: recaptcha/image/math (Optional: Default "image")
- width: width of image captcha (Optional. Applies to type "image" only)
- height: height of image captcha (Optional. Applies to type "image" only)
- theme: default/random image captcha color pattern (Optional. Applies to type "image" only ; default "default")
- length: number of characters in image captcha (Optional. Applies to type "image" only)
- angle: angle of rotation for characters in image captcha (Optional. Applies to type "image" only)
- fontAdjustment: Font size for Image Captcha (Optional. Applies to type "image" only)
- reload_txt: Reload Captcha Text (Optional)
- clabel: Label for Image Captcha field (Optional)
- mlabel: Label for Math Captcha field (Optional)
- sitekey: Googel Recaptcha Sitekey (Required. Applies to type "recatpcha" only)
(All above options can also be set from controller. Ex: $this->loadComponent('Captcha.Captcha', $options))
**Options for model. Ex: $this->addBehavior('Captcha.Captcha', $options);
- field: field name (optional: default "captcha")
- secret: Googel Recaptcha Secret (required for type "recatpcha")
Known Issues:
-
Headers already sent issue. The component uses php's
header()function to send or generate captcha image as raw HTML output. Make sure there is no output generated before the create() function in your component. It is common error to have spaces, tags or empty space in your files which would cause rending no image in the captcha. -
GD library and True Type Font (TTF) support extensions are enabled in PHP.
-
This captcha script uses three random font faces, anonymous, droidsans and ubuntu to generate fonts in the captcha images. These font faces are placed in the captcha/src/Lib/Fonts of this download. I have seen that, sometimes, these font files get corrupted during downloads. If you see font not found error in your error logs and captcha are failed to generate, try downloading these font faces from their respective sources and replace them in the mentioned folder. You can also use different font families by placing them Fonts folder and referencing them in the CaptchaComponent.php component file.
inimist/cakephp-captcha 适用场景与选型建议
inimist/cakephp-captcha 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 553 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 06 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「plugin」 「recaptcha」 「cakephp」 「captcha」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 inimist/cakephp-captcha 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 inimist/cakephp-captcha 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 inimist/cakephp-captcha 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Client library for reCAPTCHA with proxy support, a free service that protects websites from spam and abuse.
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
Provide a way to secure accesses to all routes of an symfony application.
A simple and easy to use Google ReCaptcha v3 package for Laravel
It's a barebone security class written on PHP
统计信息
- 总下载量: 553
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-06-23