kuBlai/yii2-enhanced-captcha

一个增强Yii2 captcha组件功能的扩展,只在来自同一IP的多次请求时显示

安装: 10

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

1.0.4 2020-10-23 12:16 UTC

This package is auto-updated.

Last update: 2024-09-23 22:51:43 UTC


README

一个增强Yii2 captcha组件功能的扩展,只在来自同一IP的多次请求时显示

安装

在 bower.json 文件中将包作为依赖项包含。

要安装,请运行

$ php composer.phar require jlorente/yii2-enhanced-captcha "*"

或将

...
    "require": {
        // ... other configurations ...
        "jlorente/yii2-enhanced-captcha": "*"
    }

添加到您的 composer.json 文件的 require 部分中。

使用方法

###加载模块 首先,您需要将插件作为模块包含在您的配置文件中并启动它。

<?php
//.../config/main.php
return [
    //other properties
    'modules' => [
        // list of modules,
        'captcha' => [
            'class' => 'jlorente\captcha\Module',
            //other properties initialization
        ]
    ],
    'bootstrap' => [
        //other modules to bootstrap,
        'captcha'
    ]
];

您可以通过其他方式包含它。无论如何,模块ID与使用此模块无关,因此您可以创建您想要的任何ID。有关包含模块的更多信息,请参阅Yii 2.0 definitive guide - Modules

Captcha模块使用缓存组件来存储时间戳请求队列。默认情况下,它使用Apc缓存,但您可以在模块声明中设置缓存属性来更改此行为。

<?php
//.../config/main.php
return [
    // ... other configurations ...
    'modules' => [
        // list of modules,
        'captcha' => [
            'class' => 'jlorente\captcha\Module',
            'cache' => [
                'class' => 'yii\caching\ApcCache',
                // ... other configurations for the cache component ...
            ]
            // ... other configurations for the module ...
        ]
    ],
    'bootstrap' => [
        //other modules to bootstrap,
        'captcha'
    ]
];

有关支持的缓存存储和组件初始化的更多信息,请参阅手册

可以在模块配置中设置其他属性,例如检查时间段的时间和在显示captcha之前请求的数量。

<?php
//.../config/main.php
return [
    // ... other configurations ...
    'modules' => [
        // list of modules,
        'captcha' => [
            'class' => 'jlorente\captcha\Module',
            'cache' => [
                'class' => 'yii\caching\ApcCache',
                // ... other configurations for the cache component ...
            ],
            'duration' => 100, //In seconds
            'requestNumber' => 3
            // ... other configurations for the module ...
        ]
    ],
    'bootstrap' => [
        //other modules to bootstrap,
        'captcha'
    ]
];

默认情况下,请求次数为2,时间为120。

模块还提供了一个控制器操作。CaptchaAction可以配置在模块配置参数中。请参阅手册以获取CaptchaAction配置参数的完整列表。

<?php
//.../config/main.php
return [
    // ... other configurations ...
    'modules' => [
        // list of modules,
        'captcha' => [
            'class' => 'jlorente\captcha\Module',
            'cache' => [
                'class' => 'yii\caching\ApcCache',
                // ... other configurations for the cache component ...
            ],
            'duration' => 100, //In seconds
            'requestNumber' => 3,
            'captchaAction' => [
                'class' => CaptchaAction::className(),
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
                // ... other configurations for the captcha action ...
            ]
            // ... other configurations for the module ...
        ]
    ],
    'bootstrap' => [
        //other modules to bootstrap,
        'captcha'
    ]
];

###使用小部件和验证器

配置并加载模块后,您可以使用带有模型的插件和验证器。

将CaptchaValidator类作为验证器包含在captcha属性中。

<?php
//.../models/MyModel.php
use jlorente\captcha\CaptchaValidator;

class MyModel extends \yii\base\Model {
    
    public $id;
    public $name;
    public $captcha;
    public function rules() {
        return [
            [['id', 'name'], 'required'],
            ['captcha', CaptchaValidator::className()]
        ];
    }
}

并将小部件添加到您的视图中captcha属性的ActiveField中。

<?php
//.../views/mymodel/create.php
use jlorente\captcha\Captcha;

$form = ActiveForm::begin([
            'id' => 'my-form',
]);

echo $form->field($this->model, 'id');
echo $form->field($this->model, 'name');
echo $form->field($this->model, 'captcha', [
    'template' => "{input}\n{hint}\n{error}"
])->widget(Captcha::className());

// In this example the template attribute is provided to the ActiveField in order to hide the label of the captcha attribute.

现在,只有在同一IP对当前模型有多次请求时,captcha才会显示。

##其他考虑事项

当使用CaptchaValidator时,会计算请求,因此如果表单提交时没有调用Model的validate方法,captcha将永远不会显示。

此模块是Yii 2.0框架中内置的captcha功能的扩展,以提供附加功能,因此如果您想查看更多小部件、验证器和操作选项和配置,请参阅手册

许可证

版权所有 © 2015 José Lorente Martín jose.lorente.martin@gmail.com。许可协议为MIT。有关详细信息,请参阅LICENSE.txt。