Class yii\captcha\CaptchaAction

Inheritanceyii\captcha\CaptchaAction » yii\base\Action » yii\base\Component » yii\base\Object
Implementsyii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/captcha/CaptchaAction.php

CaptchaAction renders a CAPTCHA image.

CaptchaAction is used together with yii\captcha\Captcha and yii\captcha\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 yii\captcha\Captcha widget in the form.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$backColor integer The background color. yii\captcha\CaptchaAction
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$controller yii\base\Controller|yii\web\Controller The controller that owns this action yii\base\Action
$fixedVerifyCode string The fixed verification code. yii\captcha\CaptchaAction
$fontFile string The TrueType font file. yii\captcha\CaptchaAction
$foreColor integer The font color. yii\captcha\CaptchaAction
$height integer The height of the generated CAPTCHA image. yii\captcha\CaptchaAction
$id string ID of the action yii\base\Action
$maxLength integer The maximum length for randomly generated word. yii\captcha\CaptchaAction
$minLength integer The minimum length for randomly generated word. yii\captcha\CaptchaAction
$offset integer The offset between characters. yii\captcha\CaptchaAction
$padding integer Padding around the text. yii\captcha\CaptchaAction
$testLimit integer How many times should the same CAPTCHA be displayed. yii\captcha\CaptchaAction
$transparent boolean Whether to use transparent background. yii\captcha\CaptchaAction
$uniqueId string The unique ID of this action among the whole application. yii\base\Action
$verifyCode string The verification code. yii\captcha\CaptchaAction
$width integer The width of the generated CAPTCHA image. yii\captcha\CaptchaAction

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Object
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\Action
__get() Returns the value of an object property. yii\base\Object
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Object
__set() Sets value of an object property. yii\base\Object
__unset() Sets an object property to null. yii\base\Object
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Object
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Object
className() Returns the fully qualified name of this class. yii\base\Object
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
generateValidationHash() Generates a hash code that can be used for client side validation. yii\captcha\CaptchaAction
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getUniqueId() Returns the unique ID of this action among the whole application. yii\base\Action
getVerifyCode() Gets the verification code. yii\captcha\CaptchaAction
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Object
hasProperty() Returns a value indicating whether a property is defined. yii\base\Object
init() Initializes the action. yii\captcha\CaptchaAction
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
run() Runs the action. yii\captcha\CaptchaAction
runWithParams() Runs this action with the specified parameters. yii\base\Action
trigger() Triggers an event. yii\base\Component
validate() Validates the input to see if it matches the generated code. yii\captcha\CaptchaAction

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
afterRun() This method is called right after run() is executed. yii\base\Action
beforeRun() This method is called right before run() is executed. yii\base\Action
generateVerifyCode() Generates a new verification code. yii\captcha\CaptchaAction
getSessionKey() Returns the session variable name used to store verification code. yii\captcha\CaptchaAction
renderImage() Renders the CAPTCHA image. yii\captcha\CaptchaAction
renderImageByGD() Renders the CAPTCHA image based on the code using GD library. yii\captcha\CaptchaAction
renderImageByImagick() Renders the CAPTCHA image based on the code using ImageMagick library. yii\captcha\CaptchaAction
setHttpHeaders() Sets the HTTP headers needed by image response. yii\captcha\CaptchaAction

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
REFRESH_GET_VAR 'refresh' The name of the GET parameter indicating whether the CAPTCHA image should be regenerated. yii\captcha\CaptchaAction

Property Details

$backColor public property
integer $backColor 16777215

The background color. For example, 0x55FF00. Defaults to 0xFFFFFF, meaning white color.

$fixedVerifyCode public property

The fixed verification code. When this property is set, 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 public property
string $fontFile '@yii/captcha/SpicyRice.ttf'

The TrueType font file. This can be either a file path or path alias.

$foreColor public property
integer $foreColor 2113696

The font color. For example, 0x55FF00. Defaults to 0x2040A0 (blue color).

$height public property

The height of the generated CAPTCHA image. Defaults to 50.

$maxLength public property

The maximum length for randomly generated word. Defaults to 7.

$minLength public property

The minimum length for randomly generated word. Defaults to 6.

$offset public property

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

$padding public property

Padding around the text. Defaults to 2.

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

$transparent public property

Whether to use transparent background. Defaults to false.

$verifyCode public read-only property
string getVerifyCode$regenerate false )

The verification code.

$width public property

The width of the generated CAPTCHA image. Defaults to 120.

Method Details

generateValidationHash() public method

Generates a hash code that can be used for client side validation.

string generateValidationHash$code )
$code string

The CAPTCHA code

return string

A hash code generated from the CAPTCHA code

generateVerifyCode() protected method

Generates a new verification code.

string generateVerifyCode( )
return string

The generated verification code

getSessionKey() protected method

Returns the session variable name used to store verification code.

string getSessionKey( )
return string

The session variable name

getVerifyCode() public method

Gets the verification code.

string getVerifyCode$regenerate false )
$regenerate boolean

Whether the verification code should be regenerated.

return string

The verification code.

init() public method

Initializes the action.

void init( )
throws yii\base\InvalidConfigException

if the font file does not exist.

renderImage() protected method

Renders the CAPTCHA image.

string renderImage$code )
$code string

The verification code

return string

Image contents

renderImageByGD() protected method

Renders the CAPTCHA image based on the code using GD library.

string renderImageByGD$code )
$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.

string renderImageByImagick$code )
$code string

The verification code

return string

Image contents in PNG format.

run() public method

Runs the action.

void run( )
setHttpHeaders() protected method

Sets the HTTP headers needed by image response.

void setHttpHeaders( )
validate() public method

Validates the input to see if it matches the generated code.

boolean validate$input$caseSensitive )
$input string

User input

$caseSensitive boolean

Whether the comparison should be case-sensitive

return boolean

Whether the input is valid