lubosdz/yii2-captcha-extended

Extended captcha code generator for Yii2 framework with configurable obfuscation level

Installs: 712

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 0

Open Issues: 0

Type:yii2-extension

1.0.3 2020-06-07 21:19 UTC

This package is auto-updated.

Last update: 2021-01-07 23:01:44 UTC


README

Yii2 Captcha Extended is an extension written for Yii2 framework. It enhances original captcha code delivered along with the framework - see DEMO. Version for Yii 1.x is available at Yii Framework Extensions.

Features

  • supports modes: default, math, mathverbal, words, logical
  • supports extended characters latin1, latin2 (utf-8) including middle- east- european and cyrillyc characters
  • adds visual obfuscation elements: dots density, through lines, fillSections, random text & background color
  • mode-dependent options: randomUpperLowerCase (default mode), resultMultiplier (math mode)

Screenshot Screenshot
Screenshot Screenshot
Screenshot
Screenshot

INSTALLATION

  1. Install via composer:
$ composer require "lubosdz/yii2-captcha-extended" : "~1.0.0"

or you can include the following in your composer.json file:

{
	"require": {
		"lubosdz/yii2-captcha-extended" : "~1.0.0"
	}
}
  1. Define action in controller, e.g. SiteController:
public function actions()
{
	return [
		'captcha' => [
			'class' => 'lubosdz\captchaExtended\CaptchaExtendedAction',
			// optionally, set mode and obfuscation properties e.g.:
			'mode' => 'math',
			//'mode' => CaptchaExtendedAction::MODE_MATH,
			//'resultMultiplier' => 5,
			//'lines' => 5,
			//'density' => 10,
			//'height' => 50,
			//'width' => 150,
		],
	];
}
  1. Define client validation in LoginForm::rules():
public $verifyCode;

public function rules()
{
	return [
		['verifyCode', 'lubosdz\captchaExtended\CaptchaExtendedValidator',
			'captchaAction' => Url::to('/site/captcha'),
		],
	];
}
  1. In view defined captcha field inside login form e.g.:
<?php $form = ActiveForm::begin() ?>

// ...

<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
	'captchaAction' => Url::to('/site/captcha'),
	'template' => '<div class="text-center">{image}</div><br/> {input} ',
	'imageOptions' => [
		'class' => 'img-fluid',
		'style' => 'cursor:pointer; width: 100%',
		'title' => Yii::t('app', 'Click to refresh the code'),
	],
	'options' => [
		'placeholder' => Yii::t('app', 'Verification code'),
		'class' => 'form-control',
	],
])->label(false) ?>

// ...

<?php ActiveForm::end(); ?>
  1. If needed, collect localized strings via CLI command yiic message messages/config.php and translate captcha related strings.

  2. Since by default capchta configures to default framework's settings, you may want to adjust some options:

    • mode - default|math|mathverbal|logical|words,
      • for the words mode, you can replace your own file [words.txt] or [words.yourlanguage.txt]
    • density - dots density [0-100],
    • lines - the number of through lines [0-20],
    • fillSections - the number of fillSections [0-20],
    • letters - define your own first characters set (UTF-8 supported)
    • vowels - define your own second characters set (UTF-8 supported)
    • resultMultiplier - applied to math mode to increase formula difficulty
    • fileWords - abs. path to file with your own defined locale words (UTF-8 supported)
    • randomUpperLowerCase - mix up randomly upper & lower characters from characters sets
    • paramRefreshUrl - set your own URL refresh param, defaults to v
    • also note standard properties supported by framework: width, height, padding, offset, foreColor, backColor, transparent, minLength, maxLength, ..
  3. Enjoy! :-)