On this page
Class yii\captcha\CaptchaAction
Inheritance | yii\captcha\CaptchaAction » yii\base\Action » yii\base\Component » yii\base\Object |
---|---|
Implements | yii\base\Configurable |
Available since version | 2.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:
- Override yii\web\Controller::actions() and register an action of class CaptchaAction with ID 'captcha'
- In the form model, declare an attribute to store user-entered verification code, and declare the attribute to be validated by the 'captcha' validator.
- In the controller view, insert a yii\captcha\Captcha widget in the form.
Public Properties
Public Methods
Method | Description | Defined 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
Method | Description | Defined 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
Constant | Value | Description | Defined 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
The background color. For example, 0x55FF00. Defaults to 0xFFFFFF, meaning white color.
public integer $backColor = 0xFFFFFF
$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.
public string $fixedVerifyCode = null
$fontFile public property
The TrueType font file. This can be either a file path or path alias.
public string $fontFile = '@yii/captcha/SpicyRice.ttf'
$foreColor public property
The font color. For example, 0x55FF00. Defaults to 0x2040A0 (blue color).
public integer $foreColor = 0x2040A0
$height public property
The height of the generated CAPTCHA image. Defaults to 50.
public integer $height = 50
$imageLibrary public property (available since version 2.0.7)
The rendering library to use. Currently supported only 'gd' and 'imagick'. If not set, library will be determined automatically.
public string $imageLibrary = null
$maxLength public property
The maximum length for randomly generated word. Defaults to 7.
public integer $maxLength = 7
$minLength public property
The minimum length for randomly generated word. Defaults to 6.
public integer $minLength = 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.
public integer $offset = -2
$padding public property
Padding around the text. Defaults to 2.
public integer $padding = 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).
public integer $testLimit = 3
$transparent public property
Whether to use transparent background. Defaults to false.
public boolean $transparent = false
$verifyCode public read-only property
The verification code.
public string getVerifyCode ( $regenerate = false )
$width public property
The width of the generated CAPTCHA image. Defaults to 120.
public integer $width = 120
Method Details
generateValidationHash() public method
Generates a hash code that can be used for client-side validation.
public 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.
protected string generateVerifyCode ( ) | ||
---|---|---|
return | string | The generated verification code |
getSessionKey() protected method
Returns the session variable name used to store verification code.
protected string getSessionKey ( ) | ||
---|---|---|
return | string | The session variable name |
getVerifyCode() public method
Gets the verification code.
public string getVerifyCode ( $regenerate = false ) | ||
---|---|---|
$regenerate | boolean | Whether the verification code should be regenerated. |
return | string | The verification code. |
init() public method
Initializes the action.
public void init ( ) | ||
---|---|---|
throws | yii\base\InvalidConfigException | if the font file does not exist. |
renderImage() protected method
Renders the CAPTCHA image.
protected string renderImage ( $code ) | ||
---|---|---|
$code | string | The verification code |
return | string | Image contents |
throws | yii\base\InvalidConfigException | if imageLibrary is not supported |
renderImageByGD() protected method
Renders the CAPTCHA image based on the code using GD library.
protected 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.
protected string renderImageByImagick ( $code ) | ||
---|---|---|
$code | string | The verification code |
return | string | Image contents in PNG format. |
run() public method
Runs the action.
public void run ( ) |
---|
setHttpHeaders() protected method
Sets the HTTP headers needed by image response.
protected void setHttpHeaders ( ) |
---|
validate() public method
Validates the input to see if it matches the generated code.
© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-captcha-captchaaction.html