PHP Class yii\captcha\CaptchaAction

CaptchaAction is used together with Captcha and CaptchaValidator to provide the CAPTCHA feature. By configuring the properties of CaptchaAction, you may customize the appearance of the generated CAPTCHA images, such as the font color, the background color, etc. Note that CaptchaAction requires either GD2 extension or ImageMagick PHP extension. Using CAPTCHA involves the following steps: 1. Override [[\yii\web\Controller::actions()]] and register an action of class CaptchaAction with ID 'captcha' 2. In the form model, declare an attribute to store user-entered verification code, and declare the attribute to be validated by the 'captcha' validator. 3. In the controller view, insert a Captcha widget in the form.
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Action
Datei anzeigen Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$backColor the background color. For example, 0x55FF00. Defaults to 0xFFFFFF, meaning white color.
$fixedVerifyCode the fixed verification code. When this property is set, CaptchaAction::getVerifyCode will always return the value of this property. 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.
$fontFile the TrueType font file. This can be either a file path or path alias.
$foreColor the font color. For example, 0x55FF00. Defaults to 0x2040A0 (blue color).
$height the height of the generated CAPTCHA image. Defaults to 50.
$imageLibrary the rendering library to use. Currently supported only 'gd' and 'imagick'. If not set, library will be determined automatically.
$maxLength the maximum length for randomly generated word. Defaults to 7.
$minLength the minimum length for randomly generated word. Defaults to 6.
$offset the offset between characters. Defaults to -2. You can adjust this property in order to decrease or increase the readability of the captcha.
$padding padding around the text. Defaults to 2.
$testLimit 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).
$transparent whether to use transparent background. Defaults to false.
$width the width of the generated CAPTCHA image. Defaults to 120.

Public Methods

Method Description
generateValidationHash ( string $code ) : string Generates a hash code that can be used for client-side validation.
getVerifyCode ( boolean $regenerate = false ) : string Gets the verification code.
init ( ) Initializes the action.
run ( ) Runs the action.
validate ( string $input, boolean $caseSensitive ) : boolean Validates the input to see if it matches the generated code.

Protected Methods

Method Description
generateVerifyCode ( ) : string Generates a new verification code.
getSessionKey ( ) : string Returns the session variable name used to store verification code.
renderImage ( string $code ) : string Renders the CAPTCHA image.
renderImageByGD ( string $code ) : string Renders the CAPTCHA image based on the code using GD library.
renderImageByImagick ( string $code ) : string Renders the CAPTCHA image based on the code using ImageMagick library.
setHttpHeaders ( ) Sets the HTTP headers needed by image response.

Method Details

generateValidationHash() public method

Generates a hash code that can be used for client-side validation.
public generateValidationHash ( string $code ) : string
$code string the CAPTCHA code
return string a hash code generated from the CAPTCHA code

generateVerifyCode() protected method

Generates a new verification code.
protected generateVerifyCode ( ) : string
return string the generated verification code

getSessionKey() protected method

Returns the session variable name used to store verification code.
protected getSessionKey ( ) : string
return string the session variable name

getVerifyCode() public method

Gets the verification code.
public getVerifyCode ( boolean $regenerate = false ) : string
$regenerate boolean whether the verification code should be regenerated.
return string the verification code.

init() public method

Initializes the action.
public init ( )

renderImage() protected method

Renders the CAPTCHA image.
protected renderImage ( string $code ) : string
$code string the verification code
return string image contents

renderImageByGD() protected method

Renders the CAPTCHA image based on the code using GD library.
protected renderImageByGD ( string $code ) : string
$code string the verification code
return string image contents in PNG format.

renderImageByImagick() protected method

Renders the CAPTCHA image based on the code using ImageMagick library.
protected renderImageByImagick ( string $code ) : string
$code string the verification code
return string image contents in PNG format.

run() public method

Runs the action.
public run ( )

setHttpHeaders() protected method

Sets the HTTP headers needed by image response.
protected setHttpHeaders ( )

validate() public method

Validates the input to see if it matches the generated code.
public validate ( string $input, boolean $caseSensitive ) : boolean
$input string user input
$caseSensitive boolean whether the comparison should be case-sensitive
return boolean whether the input is valid

Property Details

$backColor public_oe property

the background color. For example, 0x55FF00. Defaults to 0xFFFFFF, meaning white color.
public $backColor

$fixedVerifyCode public_oe property

the fixed verification code. When this property is set, CaptchaAction::getVerifyCode will always return the value of this property. 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.
public $fixedVerifyCode

$fontFile public_oe property

the TrueType font file. This can be either a file path or path alias.
public $fontFile

$foreColor public_oe property

the font color. For example, 0x55FF00. Defaults to 0x2040A0 (blue color).
public $foreColor

$height public_oe property

the height of the generated CAPTCHA image. Defaults to 50.
public $height

$imageLibrary public_oe property

the rendering library to use. Currently supported only 'gd' and 'imagick'. If not set, library will be determined automatically.
Since: 2.0.7
public $imageLibrary

$maxLength public_oe property

the maximum length for randomly generated word. Defaults to 7.
public $maxLength

$minLength public_oe property

the minimum length for randomly generated word. Defaults to 6.
public $minLength

$offset public_oe property

the offset between characters. Defaults to -2. You can adjust this property in order to decrease or increase the readability of the captcha.
public $offset

$padding public_oe property

padding around the text. Defaults to 2.
public $padding

$testLimit public_oe property

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).
public $testLimit

$transparent public_oe property

whether to use transparent background. Defaults to false.
public $transparent

$width public_oe property

the width of the generated CAPTCHA image. Defaults to 120.
public $width