jlorente/yii2-enhanced-captcha

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

1.0.1 2017-02-09 12:26 UTC

This package is auto-updated.

Last update: 2024-09-10 04:36:43 UTC


README

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

安装

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

要安装,可以运行

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

或者在您的 composer.json 文件的 require 部分添加

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

使用方法

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

<?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 Cache,但您可以通过设置模块声明中的缓存属性来更改此行为。

<?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'
    ]
];

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

一旦配置并加载了模块,您就可以使用模型与 widget 和验证器。

将 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。