denis909 / yii2-captcha
1.0
2018-05-16 14:36 UTC
Requires
- yiisoft/yii2: >=2.0.4
This package is auto-updated.
Last update: 2024-09-17 02:47:23 UTC
README
#yii2 #captcha
该方法的核心在于,大多数自动化垃圾邮件机器人能识别验证码并输入到一个字段中,而我们创建第二个字段,并且视觉上使得人类容易理解需要在两个字段中输入验证码,而机器人则无法识别,它会尝试在一个字段中输入。为了简化人类的验证码识别,现在验证码只由数字组成。
这种保护方法适用于小型和中型网站,因为这些网站没有人会为它们特别开发垃圾邮件机器人。由于将字母更改为数字,验证码相对于常规验证码的密码学强度较低,如果有人决定为您的网站编写机器人,这可能甚至比使用常规验证码更容易。
安装
composer require denis909/yii2-captcha
使用示例
在表单中输出验证码
<?php
use denis909\yii\captcha\Captcha;
echo $form->field($model, 'verifyCode')->widget(Captcha::className(), [
'captchaAction' => '/site/captcha',
'options' => [
'class' => 'form-control'
],
'attribute2' => 'verifyCode2',
'attribute2Options' => [
'class' => $model->hasErrors('verifyCode')
? 'form-control ' . $form->errorCssClass
: 'form-control'
],
'containerTag' => 'div',
'containerOptions' => [
'class' => 'row gutters-xs'
],
'imageContainerTag' => 'div',
'imageContainerOptions' => [
'class' => 'col-xs-3',
'style' => 'min-width: 164px;'
],
'attributeContainerTag' => 'div',
'attributeContainerOptions' => [
'class' => 'col-xs-2',
'style' => 'padding-right: 0px;'
],
'devider' => '—',
'deviderTag' => 'div',
'deviderOptions' => [
'class' => 'col-xs-1',
'style' => 'padding-top: 15px; text-align: center;'
],
'attribute2ContainerTag' => 'div',
'attribute2ContainerOptions' => [
'class' => 'col-xs-2',
'style' => 'padding-left: 0px;'
]
]);?>
在控制器中配置验证码
public function actions()
{
return [
'captcha' => [
'class' => 'denis909\yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
'testLimit' => 10,
'width' => 150,
'height' => 49,
'offset' => 10,
'minLength' => 5,
'maxLength' => 5,
'padding' => 0,
'foreColor' => 0x4a4949,
//'resetValidCaptcha' => false
]
];
}
如果将 resetValidCaptcha 设置为 false,则需要在控制器中手动重置,提交表单后。
$captcha = $this->createAction('captcha');
$captcha->reset();
在模型中配置字段
public $verifyCode;
public $verifyCode2;
public function rules()
{
return [
[
'verifyCode',
\denis909\yii\captcha\CaptchaValidator::className(),
'attribute2' => 'verifyCode2',
'captchaAction' => '/site/captcha'
]
];
}
关于作者
你好,我叫Денис。我从事PHP网络应用程序开发大约10年。我使用Yii 2框架。精通PHP、MySQL、JavaScript/jquery)、HTML5(bootstrap)。
我的网站: denis909.spb.ru
如果您有关于我的工作的建议,请在网站上的反馈表单中留言,或者发送邮件到 denis909@mail.ru。