PHPackages                             juliardi/yii2-captcha - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. juliardi/yii2-captcha

ActiveYii2-extension[Security](/categories/security)

juliardi/yii2-captcha
=====================

Captcha library wrapper for Yii2

1.1.1(5y ago)167.9k↑33.3%5MITPHPCI failing

Since Nov 14Pushed 1y ago1 watchersCompare

[ Source](https://github.com/juliardi/yii2-captcha)[ Packagist](https://packagist.org/packages/juliardi/yii2-captcha)[ RSS](/packages/juliardi-yii2-captcha/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (4)Used By (0)

Yii2 Captcha Extension
======================

[](#yii2-captcha-extension)

[![Latest Stable Version](https://camo.githubusercontent.com/002bf9c865f7d904009243c109c9c2620ac59defc5fff736703b0d444b5715d2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a756c69617264692f796969322d636170746368613f6c6162656c3d737461626c65)](https://packagist.org/packages/juliardi/yii2-captcha)[![Total Downloads](https://camo.githubusercontent.com/d7ef166f54dbbce5035094c5fc5b22ffd69a1a4370abecca0331f11615aa4bb9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a756c69617264692f796969322d63617074636861)](https://packagist.org/packages/juliardi/yii2-captcha)[![Latest Stable Release Date](https://camo.githubusercontent.com/26d87d80a544c431329aca584a962ab1ee4d0cb7f5f579c8e2ecaf71b3238262/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652d646174652f6a756c69617264692f796969322d63617074636861)](https://github.com/juliardi/yii2-captcha)[![License](https://camo.githubusercontent.com/12573a7cfcc8434c5c37e8bccf8ab58923a2c17c4296f9212ee086da43c5ae90/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6a756c69617264692f796969322d63617074636861)](https://github.com/juliardi/yii2-captcha)

> Yii2 Captcha uses [Gregwar's Captcha library](https://github.com/Gregwar/Captcha) wrapper for Yii2.

Table of Contents
-----------------

[](#table-of-contents)

- [Yii2 Captcha Extension](#yii2-captcha-extension)
    - [Table of Contents](#table-of-contents)
    - [Instalation](#instalation)
    - [Usage](#usage)
        - [Action](#action)
        - [View](#view)
        - [Validation](#validation)

Instalation
-----------

[](#instalation)

Package is available on [Packagist](https://packagist.org/packages/juliardi/yii2-captcha), you can install it using [Composer](https://getcomposer.org).

```
composer require juliardi/yii2-captcha "*"
```

or add to the require section of your `composer.json` file.

```
"juliardi/yii2-captcha": "*"
```

Usage
-----

[](#usage)

This extension has 3 different steps. First is calling `juliardi\captcha\CaptchaAction` to provide [CAPTCHA](https://en.wikipedia.org/wiki/CAPTCHA) image - a way of preventing website spamming, then rendering CAPTCHA image in view with `juliardi\captcha\Captcha`, and validating user input against the generated CAPTCHA code with `juliardi\captcha\CaptchaValidator`.

Here is how to setup this extension for each step :

### Action

[](#action)

Add the following method into your Controller.

```
public function actions()
{
    return [
        'captcha' => [
            'class' => \juliardi\captcha\CaptchaAction::class,

            /**
             * How many times should the same CAPTCHA be displayed. Defaults to 3.
             * A value less than or equal to 0 means the test is unlimited (available since version 1.1.2).
             */
            'testLimit' => 3, // int

            /**
             * The width of the generated CAPTCHA image. Defaults to 150.
             */
            'width' => 150, // int

            /**
             * The height of the generated CAPTCHA image. Defaults to 40.
             */
            'height' => 40, // int

            /**
             * The minimum & maximum length for randomly generated word. Defaults to [5, 7] | min 5 max 7.
             *
             * If an array is provided, the first value will be used as the minimum length and the second value will be used as the maximum length.
             *
             * **Note:** The minimum length must be at least 3 and the maximum length must be at most 20.
             *
             */
            'length' => [5, 7], // int|int[] | // Random word length will be between 5 and 7 characters

            /**
             * The quality of the generated JPEG image. Valid values are 1 - 100. Defaults to 80.
             */
            'quality' => 80, // int

            /**
             * The fixed verification code. When this property is set,
             *
             * This is mainly used in automated tests where we want to be able to reproduce
             * the same verification code each time we run the tests.
             * If not set, it means the verification code will be randomly generated.
             */
            // 'fixedVerifyCode' => 'testme', // string|null
        ],
    ];
}
```

### View

[](#view)

Add the following code to your view to render CAPTCHA image and input.

The following example shows how to use this widget with a model attribute:

```
use juliardi\captcha\Captcha;

echo Captcha::widget([
    'model' => $model,
    'attribute' => 'captcha',

    // configure additional widget properties here
    /**
     * The route of the action that generates the CAPTCHA images.
     * The action represented by this route must be an action of [[CaptchaAction]].
     * Please refer to [[\yii\helpers\Url::toRoute()]] for acceptable formats.
     */
    'captchaAction' => 'site/captcha', // string|array

    /**
     * HTML attributes to be applied to the CAPTCHA image tag.
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
     */
    'imageOptions' => [], // array

    /**
     * The template for arranging the CAPTCHA image tag and the text input tag.
     * In this template, the token `{image}` will be replaced with the actual image tag,
     * while `{input}` will be replaced with the text input tag.
     */
    'template' => '{image} {input}', // string

    /**
     * HTML attributes for the input tag.
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
     */
    'options' => ['class' => 'form-control'], // array

]);
```

The following example will use the name property instead:

```
use juliardi\captcha\Captcha;

echo Captcha::widget([
    'name' => 'captcha',
]);
```

You can also use this widget in an [ActiveForm](https://www.yiiframework.com/doc/api/2.0/yii-widgets-activeform) using the [widget()](https://www.yiiframework.com/doc/api/2.0/yii-widgets-activefield#widget()-detail) method, for example like this:

```
