Class yii\captcha\CaptchaAction
Inheritance | yii\captcha\CaptchaAction » yii\base\Action » yii\base\Component » yii\base\BaseObject |
---|---|
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\Component |
__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 a component property. | yii\base\Component |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component |
__set() | Sets the value of a component property. | yii\base\Component |
__unset() | Sets a component property to be null. | yii\base\Component |
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\Component |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
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\Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component |
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
The background color. For example, 0x55FF00. Defaults to 0xFFFFFF, meaning white color.
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.
The TrueType font file. This can be either a file path or path alias.
The font color. For example, 0x55FF00. Defaults to 0x2040A0 (blue color).
The height of the generated CAPTCHA image. Defaults to 50.
The rendering library to use. Currently supported only 'gd' and 'imagick'. If not set, library will be determined automatically.
The maximum length for randomly generated word. Defaults to 7.
The minimum length for randomly generated word. Defaults to 6.
The offset between characters. Defaults to -2. You can adjust this property in order to decrease or increase the readability of the captcha.
Padding around the text. Defaults to 2.
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).
Whether to use transparent background. Defaults to false.
The verification code.
The width of the generated CAPTCHA image. Defaults to 120.
Method Details
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 |
---|
Generates a new verification code.
protected string generateVerifyCode ( ) | ||
return | string | The generated verification code |
---|
Returns the session variable name used to store verification code.
protected string getSessionKey ( ) | ||
return | string | The session variable name |
---|
Gets the verification code.
public string getVerifyCode ( $regenerate = false ) | ||
$regenerate | boolean | Whether the verification code should be regenerated. |
return | string | The verification code. |
---|
Initializes the action.
public void init ( ) | ||
throws | yii\base\InvalidConfigException | if the font file does not exist. |
---|
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 |
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. |
---|
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. |
---|
Runs the action.
public void run ( ) |
Sets the HTTP headers needed by image response.
protected void setHttpHeaders ( ) |
Validates the input to see if it matches the generated code.
public boolean validate ( $input, $caseSensitive ) | ||
$input | string | User input |
$caseSensitive | boolean | Whether the comparison should be case-sensitive |
return | boolean | Whether the input is valid |
---|